I have an NSOperation with an NSOperationQueue that has a bunch of child operations, some queued up.
I had a problem where even after calling cancelAllOperations on the queue my main method was hanging on waitUntilAllOperationsAreFinished.
Then when I set the complete flag I use for isFinished upon cancellation it no longer gets backed up in a cancelled queue.
- (BOOL)isFinished
{
return complete;
}
- (void)cancel
{
cancelled = YES;
complete = YES;
[_childOperationQueue cancelAllOperations];
}
Is this the correct behaviour, a cancelled operation should be technically finished? It seems like NSOperation needs the isFinished to be set to true before it will remove it, in thought this might allow it to 'clean up', but I don't know what the protocol is here and google didn't reveal much.
Canceling the operation just sets isCancelled
to return YES
. This means in your NSOperation block you can check for isCancelled
and prevent unnecessarily doing any work (you need to implement this logic yourself).
Your main thread needs to wait for all the operations on the queue, but if your NSOperation block checks isCancelled
before doing anything you should quickly get through all the queued operations and the wait shouldn't be long.