Class

OperationQueue

The NSOperationQueue class regulates the execution of a set of Operation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

Overview

Inter-operation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. An operation object is not considered ready to execute until all of its dependent operations have finished executing. For operations that are ready to execute, the operation queue always executes the one with the highest priority relative to the other ready operations. For details on how to set priority levels and dependencies, see Operation.

You cannot directly remove an operation from a queue after it has been added. 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.

Operation queues usually provide the threads used to run their operations. Operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as asynchronous or synchronous operations.

For more information about using operation queues, see Concurrency Programming Guide.

KVO-Compliant Properties

The NSOperationQueue class is key-value coding (KVC) and key-value observing (KVO) compliant. You can observe these properties as desired to control other parts of your application. To observe the properties, use the following key paths::

  • operations - read-only

  • operationCount - read-only

  • maxConcurrentOperationCount - readable and writable

  • suspended - readable and writable

  • name - 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. However, KVO notifications associated with an operation queue may occur in any thread.

For more information about key-value observing and how to attach observers to an object, see Key-Value Observing Programming Guide.

Multicore Considerations

It is safe to use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object.

Additional Operation Queue Behaviors

An operation queue executes its queued operation objects based on their priority and readiness. If all of the queued operation objects have the same priority and are ready to execute when they are put in the queue—that is, their isReady method returns true—they are executed in the order in which they were submitted to the queue. However, you should never rely on queue semantics to ensure a specific execution order of operation objects. Changes in the readiness of an operation can change the resulting execution order. If you need operations to execute in a specific order, use operation-level dependencies as defined by the Operation class.

Symbols

Managing Operations in the Queue

func addOperation(Operation)

Adds the specified operation object to the receiver.

func addOperations([Operation], waitUntilFinished: Bool)

Adds the specified array of operations to the queue.

func addOperation( () -> Void)

Wraps the specified block in an operation object and adds it to the receiver.

var operations: [Operation]

An array of the operations currently in the queue.

var operationCount: Int

The number of operations currently in the queue.

func cancelAllOperations()

Cancels all queued and executing operations.

func waitUntilAllOperationsAreFinished()

Blocks the current thread until all of the receiver’s queued and executing operations finish executing.

Managing the Execution of Operations

var qualityOfService: QualityOfService

The default service level to apply to operations executed using the queue.

var maxConcurrentOperationCount: Int

The maximum number of queued operations that can execute at the same time.

Suspending Operations

var isSuspended: Bool

A Boolean value indicating whether the queue is actively scheduling operations for execution.

Configuring the Queue

var underlyingQueue: DispatchQueue?

The dispatch queue used to execute operations.

var name: String?

The name of the operation queue.

Constants

Concurrent Operation Constants

Constant indicating the number of supported concurrent operations.

Relationships

Inherits From

Conforms To