WanderingScouse
WanderingScouse

Reputation: 291

operation.cancel() not cancelling current operation

Using operation.cancel() does not cancel the current operation. If there is an operation in the queue it seems to cancel it but if it isExecuting then it doesn't seem to stop it.

Do i need to send something back to the main() function in my subclassed Operation to get it to stop?

for operation in downloadQueue.operations {
    if operation.name == opName {
        if operation.isExecuting == true {
           operation.cancel()
        }
    }
}

Upvotes: 1

Views: 1685

Answers (1)

simonWasHere
simonWasHere

Reputation: 1340

See the documentation at: https://developer.apple.com/reference/foundation/operationqueue

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.

Basically your code for the operation should check isCancelled and stop if needed. For example you might have a loop processing data items and you could check isCancelled before processing each item.

Upvotes: 6

Related Questions