• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSOperationQueue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSOperationQueue


  • Subject: Re: NSOperationQueue
  • From: John Love <email@hidden>
  • Date: Fri, 19 Sep 2008 11:15:34 -0400

No sooner do I say "Solved" that I determine "Not Solved".

I am no longer crashing in part due to the fact that I've removed NSApplescript from the thread; however, the evidence is still insurmountable that the calculation is not running in a background Thread, but in the main thread. Because it's in the foreground, I lose control of my application until the Thread finishes.

If you have already answered my problem, I apologize in advance for the noise.

Some of this repeats what I've already posted, but I am going big-time overboard in an attempt to be complete:

By way of background:
1) a IBOutlet of MyDocument = MyDocController
2) a IBOutlet of MyDocController = MyCalculationController

To continue:
3) In MyDocument's windowControllerDidLoadNib I call MyDocController's MakeNewFile to which I pass the (NSURL*)theFile which is the file I just double-clicked on Cocoa's built-in open dialog. (If theFile = nil) then I open up a blank document) If theFile is not nil, then I call MyDocController's startCalculation.
4) MyDocController's startCalculation calls MyCalculationController's startCalculation.


Now, the threading "fun" begins all within MyCalculationController:
6) Its Interface looks like:
@interface MyCalculationController:NSObject {
	NSOperationQueue      *itsQueue;
	NSInvocationOperation *itsOp;
	int                   itsCalcStatus;
}

7) Its -init looks like:
- (id) init {
	if (self = [super init]) {
		itsCalcStatus = kNoError;  // an enumerated constant
		itsQueue      = [[NSOperationQueue alloc] init];
		itsOp         = nil;
	}
	return self;
}

8) Its -dealloc looks like:
- (void) dealloc {
	[itsQueue release];
	// [itsOp release];   // each Operation released after it is finished
	[super dealloc];
}

9) Its -startCalculation looks like:
- (void) startCalculation {
int row;
// itsCalcStatus = kNoError; // set by -init
for (row=1; row < 10000; row++) {
if (itsCalcStatus == kSpreadsheetStopped) break;
if (itsCalcStatus != kNoError) break;

itsCalcStatus = kSpreadsheetCalculating;
[self startOperation]; // start new thread
// if running in background, this will have no effect:
[itsQueue waitUntilAllOperationsAreFinished];
}

// After the for-loop completes, itsCalcStatus =
// kSpreadsheetCalculating, kSpreadsheetStopped, kNoExcelApp, or kNoWorkbook
// So ...
if (itsCalcStatus == kSpreadsheetCalculating) {
// no errors
[self finishCalculation];
}
else {
// either stopped or an un-recoverable error = kNoExcelApp, kNoWorkbook
[self stopOperation];
// this MyDocument is done and the only user option is to close this doc
// and open a new doc, beginning with a fresh Queue.
[itsQueue release];
}
}


10) The methods called within -startCalculation look like:

- (void) startOperation {
itsOp = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(calculateWorksheetRow:)
object:nil];
[itsQueue addOperation:itsOp];
}


- (void) finishOperation {
	[itsOp release];  // -calculateWorksheetRow completely finished
}

- (void) stopOperation {
	[itsOp cancel];
	[itsOp release];
}

11) ultimately called from MyDocument's -(IBAction) stopDocument: (id)sender:
- (void) stopCalculation {
itsCalcStatus = kSpreadsheetStopped; // -startCalculation breaks from its for-loop
}


12) Finally, my -calculateWorksheetRow looks like:
- (void) calculateWorksheetRow {
	// column after column after column ...
	[self finishOperation];
}

Again, if this is too wordy, I really apoligize for all the noise.

John Love


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSOperationQueue
      • From: Quincey Morris <email@hidden>
  • Prev by Date: how to set Document type and its icon programatically
  • Next by Date: Constant NSArray Count
  • Previous by thread: Re: NSOperationQueue
  • Next by thread: Re: NSOperationQueue
  • Index(es):
    • Date
    • Thread