I have a class. Inside this class I pass an NSOperation into the NSOperationQueue that lives in my global variables.
Now my NSOperation is finished. I just want to know that it is finished in my class and to have the operation pass data to that class. How is this typically done?
I use the delegate pattern - that was the approach recommended to me by the guys at an Apple Developer Conference.
The scaffolding:
MyOperationDelegate
with a setResult:(MyResultObject *) result
method.@property id<MyOperationDelegate> delegate;
to the NSOperation subclass you've created.The work:
[myOperation setDelegate: self];
[delegate setResult: myResultObject];
to pass the result on.Another alternative... if your need to do some work when the operation is complete, you could wrap that work up in a block and use a dependancy. This is very simple, especially with NSBlockOperation.
NSOperationQueue* myQueue = [[NSOperationQueue alloc] init];
NSBlockOperation* myOp = [NSBlockOperation blockOperationWithBlock:^{
// Do work
}];
NSBlockOperation* myOp2 = [NSBlockOperation blockOperationWithBlock:^{
// Do work
}];
// My Op2 will only start when myOp is complete
[myOp2 addDependency:myOp];
[myQueue addOperation:myOp];
[myQueue addOperation:myOp2];
Also you can use setCompletionBlock
[myOp setCompletionBlock:^{
NSLog(@"MyOp completed");
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With