Eyal
Eyal

Reputation: 10828

Objective c - Canceling operations in NSOperationQueue

I'm using AFNetworking AFHTTPClient just for the example, but this question is about NSOperationQueue in general.

The AFHTTPClient manage an NSOperationQueue for requests made by the client.

It also has a cancelAllOperations method that iterate over the self.operationQueue.operations and call [operation cancel] for each one.

If I understand this right, it will cancel all the operations waiting in the queue - meaning the operation that didn't started yet, but what with the operations that are currently running? they won't be cancelled??

Upvotes: 0

Views: 3670

Answers (1)

msk
msk

Reputation: 8905

From Apple's documentation

For currently executing operations, cancel means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.

An operation remains in its queue until it reports that it is finished with its task. Finishing its task does not necessarily mean that the operation performed that task to completion. An operation can also be canceled. Canceling an operation object leaves the object in the queue but notifies the object that it should abort its task as quickly as possible. For currently executing operations, this means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.

Upvotes: 2

Related Questions