I have a calculation task that will take some time to complete. Minutes. Maybe several minutes. I want to launch the task with a button on the UI, then return to the UI so that the user can interact with other areas while the task is running.
I modeled my code after an example from Apple’s Concurrency Programming Guide:
// code here to initialize the c++ objects a, b, c, and d as ivars.
- (NSOperation*)taskWithData:(id)data
{
NSInvocationOperation* theOp = [[[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(myTaskMethod:)
object:nil] autorelease];
return theOp;
}
// This is the method that does the actual work of the task.
- (void)myTaskMethod:(id)data
{
// Perform the task.
task(a, b, c, d);
}
Then I call myTaskMethod:data from within the button’s IBAction:
-(void)actionButtonPushed:(id)sender
{
NSAutoreleasePool* myPool = [NSAutoreleasePool alloc] init];
[self myTaskMethod:nil];
[myPool drain];
}
I hoped that control would return to the UI, but it doesn’t. We don’t return to the UI until myTaskMethod: finishes its business … exactly the same behavior as if I called task() directly in the IBAction code.
More details: this code is Objective-C++ (.mm), and task(); is a c++ function.
If it’s not obvious, this is my first attempt at using Concurrency; I need some direction, please.
tia, g
Update: Fixed. I had neglected to create an SNOperationQueue, and add my NSOperation to the queue.
Update: Fixed. I had neglected to create an SNOperationQueue, and add my NSOperation to the queue.