<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.5.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/OperationQueue",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOperationQueue"
  },
  "title" : "OperationQueue"
}
-->

# OperationQueue

A queue that regulates the execution of operations.

```
class OperationQueue
```

## Overview

An operation queue invokes its queued [`Operation`](/documentation/Foundation/Operation) objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can’t directly remove an operation from a queue after you add it.

> Note:
> Operation queues retain operations until the operations finish, and queues themselves are retained until all operations are finished. Suspending an operation queue with operations that aren’t finished can result in a memory leak.

For more information about using operation queues, see the [Concurrency Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091).

### Determine the Execution Order

An operation queue organizes and invokes its operations according to their readiness, priority level, and interoperation dependencies. If all of the queued operations have the same [`queuePriority`](/documentation/Foundation/Operation/queuePriority-swift.property) and the [`isReady`](/documentation/Foundation/Operation/isReady) property returns <doc://com.apple.documentation/documentation/Swift/true>, the queue invokes them in the order you added them. Otherwise, the operation queue always invokes the operation with the highest priority relative to the other ready operations.

However, don’t rely on queue semantics to ensure a specific execution order of operations  because changes in the readiness of an operation can change the resulting execution order. Interoperation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. An operation object isn’t ready to run until all of its dependent operations have finished running.

For details on how to set priority levels and dependencies, see Managing Dependencies in [`Operation`](/documentation/Foundation/Operation).

### Respond to Operation Cancelation

Finishing its task doesn’t 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 stop 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()`](/documentation/Foundation/Operation/start()) method so that it can processes the cancellation event and mark itself as finished.

> Note:
> Canceling an operation causes the operation to ignore any dependencies it may have. This behavior makes it possible for the queue to invoke the operation’s ``doc://com.apple.foundation/documentation/Foundation/Operation/start()`` method as soon as possible. The ``doc://com.apple.foundation/documentation/Foundation/Operation/start()`` method, in turn, moves the operation to the finished state so that it can be removed from the queue.

For more information about operation cancellation, see [`Responding to the Cancel Command`](/documentation/Foundation/Operation#Responding-to-the-Cancel-Command) in [`Operation`](/documentation/Foundation/Operation).

### Observe Operations Using Key-Value Observing

The [`OperationQueue`](/documentation/Foundation/OperationQueue) class is key-value coding (KVC) and key-value observing (KVO) compliant. You can observe these properties to control other parts of your application. To observe the properties, use the following key paths:

- [`operations`](/documentation/Foundation/OperationQueue/operations) — Read-only
- [`operationCount`](/documentation/Foundation/OperationQueue/operationCount) — Read-only
- [`maxConcurrentOperationCount`](/documentation/Foundation/OperationQueue/maxConcurrentOperationCount) — Readable and writable
- [`isSuspended`](/documentation/Foundation/OperationQueue/isSuspended) — Readable and writable
- [`name`](/documentation/Foundation/OperationQueue/name) — Readable and writable

Although you can attach observers to these properties, don’t use Cocoa bindings to bind these properties to elements of your application’s user interface. Code associated with your user interface typically must run only in your app’s main thread. However, KVO notifications associated with an operation queue may occur in any thread.

For more information about KVO and how to attach observers to an object, see the [Key-Value Observing Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i).

### Plan for Thread Safety

You can safely use a single [`OperationQueue`](/documentation/Foundation/OperationQueue) object from multiple threads without creating additional locks to synchronize access to that object.

Operation queues use the <doc://com.apple.documentation/documentation/Dispatch> framework to initiate the execution of their operations. As a result, queues always invoke operations on a separate thread, regardless of whether the operation is synchronous or asynchronous.

## Topics

### Accessing Specific Operation Queues

[`main`](/documentation/Foundation/OperationQueue/main)

Returns the operation queue associated with the main thread.

[`current`](/documentation/Foundation/OperationQueue/current)

Returns the operation queue that launched the current operation.

### Managing Operations in the Queue

[`addOperation(_:)`](/documentation/Foundation/OperationQueue/addOperation(_:)-64o8a)

Adds the specified operation to the receiver.

[`addOperations(_:waitUntilFinished:)`](/documentation/Foundation/OperationQueue/addOperations(_:waitUntilFinished:))

Adds the specified operations to the queue.

[`addOperation(_:)`](/documentation/Foundation/OperationQueue/addOperation(_:)-5s294)

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

[`addBarrierBlock(_:)`](/documentation/Foundation/OperationQueue/addBarrierBlock(_:))

Invokes a block when the queue finishes all enqueued operations, and prevents subsequent operations from starting until the block has completed.

[`cancelAllOperations()`](/documentation/Foundation/OperationQueue/cancelAllOperations())

Cancels all queued and executing operations.

[`waitUntilAllOperationsAreFinished()`](/documentation/Foundation/OperationQueue/waitUntilAllOperationsAreFinished())

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

[`operations`](/documentation/Foundation/OperationQueue/operations)

The operations currently in the queue.

[`operationCount`](/documentation/Foundation/OperationQueue/operationCount)

The number of operations currently in the queue.

### Managing the Execution of Operations

[`qualityOfService`](/documentation/Foundation/OperationQueue/qualityOfService)

The default service level to apply to operations that the queue invokes.

[`maxConcurrentOperationCount`](/documentation/Foundation/OperationQueue/maxConcurrentOperationCount)

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

[`defaultMaxConcurrentOperationCount`](/documentation/Foundation/OperationQueue/defaultMaxConcurrentOperationCount)

The default maximum number of operations to invoke concurrently in a queue.

### Monitoring Progress of Operations

[`progress`](/documentation/Foundation/OperationQueue/progress)

An object that represents the total progress of the operations executing in the queue.

### Suspending Execution

[`isSuspended`](/documentation/Foundation/OperationQueue/isSuspended)

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

### Configuring the Queue

[`name`](/documentation/Foundation/OperationQueue/name)

The name of the operation queue.

[`underlyingQueue`](/documentation/Foundation/OperationQueue/underlyingQueue)

The dispatch queue that the operation queue uses to invoke operations.

### Scheduling Operations

[`schedule(after:tolerance:options:_:)`](/documentation/Foundation/OperationQueue/schedule(after:tolerance:options:_:))

Performs the action at some time after the specified date, optionally taking into account tolerance if possible.

[`schedule(after:interval:tolerance:options:_:)`](/documentation/Foundation/OperationQueue/schedule(after:interval:tolerance:options:_:))

Performs the action at some time after the specified date, at the specified frequency, optionally taking into account tolerance if possible.

[`schedule(options:_:)`](/documentation/Foundation/OperationQueue/schedule(options:_:))

Performs the action at the next possible opportunity.

[`now`](/documentation/Foundation/OperationQueue/now)

The operation queue’s definition of the current moment in time.

[`minimumTolerance`](/documentation/Foundation/OperationQueue/minimumTolerance)

The minimum tolerance the dispatch queue scheduler allows.

[`OperationQueue.SchedulerTimeType`](/documentation/Foundation/OperationQueue/SchedulerTimeType)

The scheduler time type the operation queue uses.

[`OperationQueue.SchedulerOptions`](/documentation/Foundation/OperationQueue/SchedulerOptions)

A type that defines options the operation queue accepts.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)