tags:

views:

492

answers:

3

I have set up an operation queue and an invocation operation. Do I need to signal that the invocation is commpleted? If not how will the operation queue knows the invocation is finished and move on to the next one? The operation queue has been set to execute one operation at a time.

+2  A: 

No, there is no need to signal that the invocation has been completed. An NSOperationQueue knows that an operation is finished when its isFinished property is set to YES. This happens by default when the operation's -main method returns.

NSInvocationOperation's -main method, for all intents and purposes, just invokes its NSInvocation and returns, so its isFinished flag should be set to YES immediately after the invocation completes.

kperryua
My task can take as long as 7 secs to finish (writing image to camera roll), so I don't want too many of these run at the same time as it would overload the memory. What should I do to make the operation not move on until the image saving is finished. The image saving operation has a callback indicating the image is done saving.
Boon
Just use [theQueue setMaxConcurrentOperationCount:1] and only one will be done at a time.
Tom Dalling
+1  A: 

Boon,

It seems like what you really want here is to subclass NSOperation yourself and call your asynchronous inside of it. When the async code completes and you get your callback, you would then notify the queue via KVO that isExecuting and isFinished are updated. This is explained much more in detail over at Dave Dribin's blog:

http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent%5Foperations/

Steven Degutis
A: 

It's automatic for NSInvocationOperation. You're already good to go.

If you need to tell other parts of your app that the operation has completed, you can use a notification. Be sure the notification goes to the right thread. On the iPhone, I send them to the main thread because I often change the UI in response to a notification, and all UI stuff must happen on the main thread.

[self performSelectorOnMainThread:@selector(postOpDoneNote) withObject:nil waitUntilDone:NO];

-(void) postOpDoneNote
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"someOpDone" object:self];
}
Paul Scott