I have some NSOperation
s in a dependency graph:
NSOperation *op1 = ...;
NSOperation *op2 = ...;
[op2 addDependency:op1];
Here's how I'm running them:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:op1];
[queue addOperation:op2];
Now I need to cancel them. How do I ensure that all the NSOperation
s in a dependency graph are cancelled, and that no other NSOperation
s are cancelled?
what I've tried:
Calling cancel
on either NSOperation
doesn't cancel the other (as far as I can tell):
[op1 cancel]; // doesn't cancel op2
// -- or --
[op2 cancel]; // doesn't cancel op1
Cancelling the queue would also cancel operations that aren't part of the dependency graph of op1
and op2
(if there are any such operations in the queue):
[queue cancelAllOperations];
So I solved this using a custom method that recursively looks through an NSOperation
's dependencies and cancels them. However, I'm not happy with this solution because I feel like I'm fighting the framework:
- (void)recursiveCancel:(NSOperation *)op
{
[op cancel];
for (NSOperation *dep in op.dependencies)
{
[self recursiveCancel:op];
}
}