2
votes

I have several operations in one queue and set dependency one by one. If one operation is failed, I hope to cancel all following operations. But after invoke [queue cancelAllOperations], the operations won't be removed from the queue or stop them. Canceling the operations does not automatically remove them from the queue or stop those that are currently executing. For operations that are queued and waiting execution, the queue must still attempt to execute the operation before recognizing that it is canceled and moving it to the finished state. How could I cancel all remaining operations? Thanks.

1

1 Answers

2
votes

NSOperation have a cancelled property that can be set explicitly on a single operation by you or by NSOperationQueue in response to a cancelAllOperations message. Operations are expected to check this property in their main method. For operations that will complete quickly, it isn't worth implementing this check. If an operation will take a long time (several seconds or longer), it should periodically do something like this:

- (void)main {
    \\ ...do work... 

    if (self.isCancelled) {
      \\ ...get out...  
    } 
 } 

This blog beautifully explains the concept!