NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, you do not use this class directly but instead subclass or use one of the system-defined subclasses (
BlockOperation) to perform the actual task. Despite being abstract, the base implementation of
NSOperation does include significant logic to coordinate the safe execution of your task. The presence of this built-in logic allows you to focus on the actual implementation of your task, rather than on the glue code needed to ensure it works correctly with other system objects.
- iOS 2.0+
- macOS 10.5+
- tvOS 2.0+
- watchOS 2.0+
An operation object is a single-shot object—that is, it executes its task once and cannot be used to execute it again. You typically execute operations by adding them to an operation queue (an instance of the
OperationQueue class). An operation queue executes its operations either directly, by running them on secondary threads, or indirectly using the
libdispatch library (also known as Grand Central Dispatch). For more information about how queues execute operations, see
If you do not want to use an operation queue, you can execute an operation yourself by calling its
start() method directly from your code. Executing operations manually does put more of a burden on your code, because starting an operation that is not in the ready state triggers an exception. The
isReady property reports on the operation’s readiness.
Dependencies are a convenient way to execute operations in a specific order. You can add and remove dependencies for an operation using the
removeDependency(_:) methods. By default, an operation object that has dependencies is not considered ready until all of its dependent operation objects have finished executing. Once the last dependent operation finishes, however, the operation object becomes ready and able to execute.
The dependencies supported by
NSOperation make no distinction about whether a dependent operation finished successfully or unsuccessfully. (In other words, canceling an operation similarly marks it as finished.) It is up to you to determine whether an operation with dependencies should proceed in cases where its dependent operations were cancelled or did not complete their task successfully. This may require you to incorporate some additional error tracking capabilities into your operation objects.
NSOperation class is key-value coding (KVC) and key-value observing (KVO) compliant for several of its properties. As needed, you can observe these properties to control other parts of your application. To observe the properties, use the following key paths:
queuePriority- readable and writable
completionBlock- readable and writable
Although you can attach observers to these properties, you should not use Cocoa bindings to bind them to elements of your application’s user interface. Code associated with your user interface typically must execute only in your application’s main thread. Because an operation may execute in any thread, KVO notifications associated with that operation may similarly occur in any thread.
If you provide custom implementations for any of the preceding properties, your implementations must maintain KVC and KVO compliance. If you define additional properties for your
NSOperation objects, it is recommended that you make those properties KVC and KVO compliant as well. For information on how to support key-value coding, see Key-Value Coding Programming Guide. For information on how to support key-value observing, see Key-Value Observing Programming Guide.
NSOperation class is itself multicore aware. It is therefore safe to call the methods of an
NSOperation object from multiple threads without creating additional locks to synchronize access to the object. This behavior is necessary because an operation typically runs in a separate thread from the one that created and is monitoring it.
When you subclass
NSOperation, you must make sure that any overridden methods remain safe to call from multiple threads. If you implement custom methods in your subclass, such as custom data accessors, you must also make sure those methods are thread-safe. Thus, access to any data variables in the operation must be synchronized to prevent potential data corruption. For more information about synchronization, see Threading Programming Guide.
Asynchronous Versus Synchronous Operations
If you plan on executing an operation object manually, instead of adding it to a queue, you can design your operation to execute in a synchronous or asynchronous manner. Operation objects are synchronous by default. In a synchronous operation, the operation object does not create a separate thread on which to run its task. When you call the
start() method of a synchronous operation directly from your code, the operation executes immediately in the current thread. By the time the
start() method of such an object returns control to the caller, the task itself is complete.
When you call the
start() method of an asynchronous operation, that method may return before the corresponding task is completed. An asynchronous operation object is responsible for scheduling its task on a separate thread. The operation could do that by starting a new thread directly, by calling an asynchronous method, or by submitting a block to a dispatch queue for execution. It does not actually matter if the operation is ongoing when control returns to the caller, only that it could be ongoing.
If you always plan to use queues to execute your operations, it is simpler to define them as synchronous. If you execute operations manually, though, you might want to define your operation objects as asynchronous. Defining an asynchronous operation requires more work, because you have to monitor the ongoing state of your task and report changes in that state using KVO notifications. But defining asynchronous operations is useful in cases where you want to ensure that a manually executed operation does not block the calling thread.
When you add an operation to an operation queue, the queue ignores the value of the
isAsynchronous property and always calls the
start() method from a separate thread. Therefore, if you always run operations by adding them to an operation queue, there is no reason to make them asynchronous.
For information on how to define both synchronous and asynchronous operations, see the subclassing notes.
NSOperation class provides the basic logic to track the execution state of your operation but otherwise must be subclassed to do any real work. How you create your subclass depends on whether your operation is designed to execute concurrently or non-concurrently.
Methods to Override
For non-concurrent operations, you typically override only one method:
Into this method, you place the code needed to perform the given task. Of course, you should also define a custom initialization method to make it easier to create instances of your custom class. You might also want to define getter and setter methods to access the data from the operation. However, if you do define custom getter and setter methods, you must make sure those methods can be called safely from multiple threads.
If you are creating a concurrent operation, you need to override the following methods and properties at a minimum:
In a concurrent operation, your
start() method is responsible for starting the operation in an asynchronous manner. Whether you spawn a thread or call an asynchronous function, you do it from this method. Upon starting the operation, your
start() method should also update the execution state of the operation as reported by the
isExecuting property. You do this by sending out KVO notifications for the
isExecuting key path, which lets interested clients know that the operation is now running. Your
isExecuting property must also provide the status in a thread-safe manner.
Upon completion or cancellation of its task, your concurrent operation object must generate KVO notifications for both the
isFinished key paths to mark the final change of state for your operation. (In the case of cancellation, it is still important to update the
isFinished key path, even if the operation did not completely finish its task. Queued operations must report that they are finished before they can be removed from a queue.) In addition to generating KVO notifications, your overrides of the
isFinished properties should also continue to report accurate values based on the state of your operation.
For additional information and guidance on how to define concurrent operations, see Concurrency Programming Guide.
Even for concurrent operations, there should be little need to override methods other than those described above. However, if you customize the dependency features of operations, you might have to override additional methods and provide additional KVO notifications. In the case of dependencies, this would likely only require providing notifications for the
isReady key path. Because the
dependencies property contains the list of dependent operations, changes to it are already handled by the default
Maintaining Operation Object States
Operation objects maintain state information internally to determine when it is safe to execute and also to notify external clients of the progression through the operation’s life cycle. Your custom subclasses must maintain this state information to ensure the correct execution of operations in your code.
Table 1 lists the key paths associated with an operation’s states and how you should manage that key path in any custom subclasses.
In most cases, you do not have to manage the state of this key path yourself. If the readiness of your operations is determined by factors other than dependent operations, however—such as by some external condition in your program—you can provide your own implementation of the
In macOS 10.6 and later, if you cancel an operation while it is waiting on the completion of one or more dependent operations, those dependencies are thereafter ignored and the value of this property is updated to reflect that it is now ready to run. This behavior gives an operation queue the chance to flush cancelled operations out of its queue more quickly.
If you replace the
If you replace the
Responding to the Cancel Command
Once you add an operation to a queue, the operation is out of your hands. The queue takes over and handles the scheduling of that task. However, if you decide later that you do not want to execute the operation after all—because the user pressed a cancel button in a progress panel or quit the application, for example—you can cancel the operation to prevent it from consuming CPU time needlessly. You do this by calling the
cancel() method of the operation object itself or by calling the
cancelAllOperations() method of the
Canceling an operation does not immediately force it to stop what it is doing. Although respecting the value in the
isCancelled property is expected of all operations, your code must explicitly check the value in this property and abort as needed. The default implementation of
NSOperation includes checks for cancellation. For example, if you cancel an operation before its
start() method is called, the
start() method exits without starting the task.
You should always support cancellation semantics in any custom code you write. In particular, your main task code should periodically check the value of the
isCancelled property. If the property reports the value
true, your operation object should clean up and exit as quickly as possible. If you implement a custom
start() method, that method should include early checks for cancellation and behave appropriately. Your custom
start() method must be prepared to handle this type of early cancellation.
In addition to simply exiting when an operation is cancelled, it is also important that you move a cancelled operation to the appropriate final state. Specifically, if you manage the values for the
isExecuting properties yourself (perhaps because you are implementing a concurrent operation), you must update those properties accordingly. Specifically, you must change the value returned by
true and the value returned by
false. You must make these changes even if the operation was cancelled before it started executing.