Swift Concurrency Patterns
Comprehensive guide for Swift concurrency covering async/await, structured concurrency, actors, and the Swift 6.2 "Approachable Concurrency" features. Focuses on patterns that prevent data races and common mistakes that cause crashes.
When This Skill Activates
- User has data race errors or actor isolation compiler errors
- User is migrating to Swift 6 strict concurrency
- User asks about async/await, actors, Sendable, TaskGroup, or MainActor
- User needs to bridge legacy completion-handler APIs to async/await
- User is working with Swift 6.2 features (@concurrent, isolated conformances)
- User has concurrency bugs (actor reentrancy, task cancellation, UI freezes)
Decision Tree
What concurrency problem are you solving?
│
├─ Swift 6 compiler errors / migration
│ └─ migration-guide.md
│
├─ Swift 6.2 new features (@concurrent, isolated conformances)
│ └─ swift62-concurrency.md
│
├─ Running work in parallel (async let, TaskGroup)
│ └─ structured-concurrency.md
│
├─ Thread safety for shared mutable state
│ └─ actors-and-isolation.md
│
├─ Bridging old APIs (delegates, callbacks) to async/await
│ └─ continuations-bridging.md
│
├─ Hangs, slow parallelism, thread-pool problems, profiling
│ └─ concurrency-internals.md
│
└─ General async/await patterns
└─ See macos/coding-best-practices/modern-concurrency.md for basics
Quick Reference
| Pattern |
When to Use |
Reference |
async let |
Fixed number of parallel operations |
structured-concurrency.md |
withTaskGroup |
Dynamic number of parallel operations |
structured-concurrency.md |
withDiscardingTaskGroup |
Fire-and-forget parallel operations |
structured-concurrency.md |
.task { } modifier |
Load data when view appears |
structured-concurrency.md |
.task(id:) modifier |
Re-load when a value changes |
structured-concurrency.md |
actor |
Shared mutable state protection |
actors-and-isolation.md |
@MainActor |
UI-bound state and updates |
actors-and-isolation.md |
@concurrent |
Explicitly offload to background (6.2) |
swift62-concurrency.md |
| Isolated conformances |
@MainActor type conforming to protocol (6.2) |
swift62-concurrency.md |
withCheckedContinuation |
Bridge callback API to async |
continuations-bridging.md |
AsyncStream |
Bridge delegate/notification API to async sequence |
continuations-bridging.md |
@TaskLocal |
Context (IDs, tracing) down the task tree |
structured-concurrency.md |
Atomic / Mutex |
Sendable wrappers for synchronous hot paths (Swift 6) |
actors-and-isolation.md |
| Custom actor executors |
Bridge existing DispatchSerialQueue synchronization |
concurrency-internals.md |
| Swift Concurrency Instrument |
Diagnose hangs, actor contention, continuation leaks |
concurrency-internals.md |
| Strict concurrency migration |
Incremental Swift 6 adoption |
migration-guide.md |
Process
1. Identify the Problem
Read the user's code or error messages to determine:
- Is this a compiler error (strict concurrency) or a runtime issue (data race, crash)?
- What Swift version and concurrency checking level are they using?
- Are they migrating existing code or writing new code?
2. Load Relevant Reference Files
Based on the problem, read from this directory:
swift62-concurrency.md — Swift 6.2 approachable concurrency features, Apple's adoption doctrine, Swift 6.3/6.4 additions
structured-concurrency.md — async let, TaskGroup, .task modifier lifecycle, task-locals, cancellation semantics
actors-and-isolation.md — Actor patterns, reentrancy, @MainActor, Sendable, Atomic/Mutex
concurrency-internals.md — cooperative pool, unsafe primitives, actor scheduling, Swift Concurrency Instrument
continuations-bridging.md — withCheckedContinuation, AsyncStream, built-in async sequences, legacy bridging
migration-guide.md — Incremental Swift 6 strict concurrency adoption (WWDC24 doctrine)
3. Review Checklist
4. Cross-Reference
- For async/await basics and actor fundamentals, see
macos/coding-best-practices/modern-concurrency.md
- For networking concurrency patterns, see
generators/networking-layer/networking-patterns.md
- For SwiftData concurrency (@ModelActor), see
macos/swiftdata-architecture/repository-pattern.md
- For auth token refresh with actors, see
generators/auth-flow/auth-patterns.md
References
1---
2name: concurrency-patterns
3description: Swift concurrency patterns including Swift 6.2 approachable concurrency, structured concurrency, actors, continuations, and migration. Use when reviewing or building async code, fixing data race errors, or migrating to Swift 6.
4allowed-tools: [Read, Glob, Grep]
5last_verified: 2026-07-16
6review_by: 2027-06-22
7os_version: iOS 27 / macOS 27
8---
9
10# Swift Concurrency Patterns
11
12Comprehensive guide for Swift concurrency covering async/await, structured concurrency, actors, and the Swift 6.2 "Approachable Concurrency" features. Focuses on patterns that prevent data races and common mistakes that cause crashes.
13
14## When This Skill Activates
15
16- User has data race errors or actor isolation compiler errors
17- User is migrating to Swift 6 strict concurrency
18- User asks about async/await, actors, Sendable, TaskGroup, or MainActor
19- User needs to bridge legacy completion-handler APIs to async/await
20- User is working with Swift 6.2 features (@concurrent, isolated conformances)
21- User has concurrency bugs (actor reentrancy, task cancellation, UI freezes)
22
23## Decision Tree
24
25```
26What concurrency problem are you solving?
27│
28├─ Swift 6 compiler errors / migration
29│ └─ migration-guide.md
30│
31├─ Swift 6.2 new features (@concurrent, isolated conformances)
32│ └─ swift62-concurrency.md
33│
34├─ Running work in parallel (async let, TaskGroup)
35│ └─ structured-concurrency.md
36│
37├─ Thread safety for shared mutable state
38│ └─ actors-and-isolation.md
39│
40├─ Bridging old APIs (delegates, callbacks) to async/await
41│ └─ continuations-bridging.md
42│
43├─ Hangs, slow parallelism, thread-pool problems, profiling
44│ └─ concurrency-internals.md
45│
46└─ General async/await patterns
47 └─ See macos/coding-best-practices/modern-concurrency.md for basics
48```
49
50## Quick Reference
51
52| Pattern | When to Use | Reference |
53|---------|-------------|-----------|
54| `async let` | Fixed number of parallel operations | `structured-concurrency.md` |
55| `withTaskGroup` | Dynamic number of parallel operations | `structured-concurrency.md` |
56| `withDiscardingTaskGroup` | Fire-and-forget parallel operations | `structured-concurrency.md` |
57| `.task { }` modifier | Load data when view appears | `structured-concurrency.md` |
58| `.task(id:)` modifier | Re-load when a value changes | `structured-concurrency.md` |
59| `actor` | Shared mutable state protection | `actors-and-isolation.md` |
60| `@MainActor` | UI-bound state and updates | `actors-and-isolation.md` |
61| `@concurrent` | Explicitly offload to background (6.2) | `swift62-concurrency.md` |
62| Isolated conformances | `@MainActor` type conforming to protocol (6.2) | `swift62-concurrency.md` |
63| `withCheckedContinuation` | Bridge callback API to async | `continuations-bridging.md` |
64| `AsyncStream` | Bridge delegate/notification API to async sequence | `continuations-bridging.md` |
65| `@TaskLocal` | Context (IDs, tracing) down the task tree | `structured-concurrency.md` |
66| `Atomic` / `Mutex` | Sendable wrappers for synchronous hot paths (Swift 6) | `actors-and-isolation.md` |
67| Custom actor executors | Bridge existing DispatchSerialQueue synchronization | `concurrency-internals.md` |
68| Swift Concurrency Instrument | Diagnose hangs, actor contention, continuation leaks | `concurrency-internals.md` |
69| Strict concurrency migration | Incremental Swift 6 adoption | `migration-guide.md` |
70
71## Process
72
73### 1. Identify the Problem
74
75Read the user's code or error messages to determine:
76- Is this a compiler error (strict concurrency) or a runtime issue (data race, crash)?
77- What Swift version and concurrency checking level are they using?
78- Are they migrating existing code or writing new code?
79
80### 2. Load Relevant Reference Files
81
82Based on the problem, read from this directory:
83- `swift62-concurrency.md` — Swift 6.2 approachable concurrency features, Apple's adoption doctrine, Swift 6.3/6.4 additions
84- `structured-concurrency.md` — async let, TaskGroup, .task modifier lifecycle, task-locals, cancellation semantics
85- `actors-and-isolation.md` — Actor patterns, reentrancy, @MainActor, Sendable, Atomic/Mutex
86- `concurrency-internals.md` — cooperative pool, unsafe primitives, actor scheduling, Swift Concurrency Instrument
87- `continuations-bridging.md` — withCheckedContinuation, AsyncStream, built-in async sequences, legacy bridging
88- `migration-guide.md` — Incremental Swift 6 strict concurrency adoption (WWDC24 doctrine)
89
90### 3. Review Checklist
91
92- [ ] No blocking calls on `@MainActor` (use `await` for long operations)
93- [ ] Shared mutable state protected by an actor (not locks or DispatchQueue)
94- [ ] `Sendable` conformance correct for types crossing isolation boundaries
95- [ ] Task cancellation handled (check `Task.isCancelled` or `Task.checkCancellation()`)
96- [ ] No unstructured `Task {}` where structured concurrency (`.task`, `TaskGroup`) would work
97- [ ] Actor reentrancy considered at suspension points
98- [ ] `withCheckedContinuation` called exactly once (not zero, not twice)
99- [ ] `.task(id:)` used instead of manual `onChange` + cancel patterns
100
101### 4. Cross-Reference
102
103- For **async/await basics and actor fundamentals**, see `macos/coding-best-practices/modern-concurrency.md`
104- For **networking concurrency patterns**, see `generators/networking-layer/networking-patterns.md`
105- For **SwiftData concurrency** (@ModelActor), see `macos/swiftdata-architecture/repository-pattern.md`
106- For **auth token refresh with actors**, see `generators/auth-flow/auth-patterns.md`
107
108## References
109
110- [Swift Concurrency](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/)
111- [Migrating to Swift 6](https://www.swift.org/migration/documentation/migrationguide/)
112- Local captured doc (optional): `~/Downloads/docs/Swift-Concurrency-Updates.md` — read if present; skip silently if absent.