Cancellation

Responding to cancellation

NSOperation instances can be cancelled, responding to cancellation effecting will ensure that overall performance of the queue and application is optimal. There are a number of situations which require attention regarding cancellation

Check whether the operation has been cancelled

Before doing work inside the Operation subclass, it is a best practice to check whether the operation has already been cancelled. Let's revisit our first Operation subclass, and do it properly :)

import Operations

class MyFirstOperation: Operation {
    override func execute() {
        guard !cancelled else { return }
        print("Hello World")
        finish()
    }
}

Did this page help you?