简体   繁体   中英

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()
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM