I have some NSOperation in the dependency graph:
NSOperation *op1 = ...; NSOperation *op2 = ...; [op2 addDependency:op1];
Here's how I run them:
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:op1]; [queue addOperation:op2];
Now I need to cancel them. How can I guarantee that all NSOperation in the dependency graph will be canceled and that no other NSOperation will be canceled?
what i tried:
Calling cancel on NSOperation does not cancel the other (as far as I can tell):
[op1 cancel];
Canceling a queue will also result in canceling operations that are not part of the dependency graph op1 and op2 (if there are such operations in the queue):
[queue cancelAllOperations]
So I solved this with a custom method that recursively looks at NSOperation dependencies and undo them. However, I am not happy with this decision, because I feel that I am struggling with the framework:
- (void)recursiveCancel:(NSOperation *)op { [op cancel]; for (NSOperation *dep in op.dependencies) { [self recursiveCancel:op]; } }
source share