10.2 · Operation, OperationQueue, dependencies and cancellation
OperationQueue is GCD with a vocabulary: units of work that have dependencies, a state and the ability to be cancelled. It is still the right tool when those matter.
Mental model. An operation is a unit of work with a lifecycle. Use it when you need to order, limit, observe or cancel work, and use a plain queue when you do not.
Operations
Dependencies, lifecycle and cancellation, and how it compares to GCD.
- When do you use OperationQueue instead of GCD, and how do dependencies, state and cancellation work?
An Operation (usually a BlockOperation or your subclass) has a state machine: ready, executing, finished, with isCancelled as a flag. An OperationQueue runs…
More in Concurrency
- 10.1 · Queues, QoS, barriers, groups, semaphores and sources
- 10.3 · async/await, tasks, task groups, cancellation and task locals
- 10.4 · Actors, reentrancy, isolated, nonisolated, global actors and @MainActor
- 10.5 · Region-based isolation, sending, and migrating to Swift 6
- 10.6 · Continuations and bridging GCD, Combine and callback APIs
- 10.7 · Deadlocks, main-actor hops, Thread Sanitizer and Instruments
- 10.8 · Task groups, async let, cancellation and task locals in practice
- 10.9 · Isolation rules, isolated parameters, global actors, main-actor hops and default isolation
- 10.10 · Sendable rules, locks versus actors, and the escape hatches
- 10.11 · Groups, semaphores, parallel loops, operation dependencies, QoS and timers
- 10.12 · AsyncStream buffering, continuation misuse and notifications as async sequences