engsys 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/core/agents/aaron.md +152 -0
- package/core/agents/bert.md +115 -0
- package/core/agents/isabelle.md +136 -0
- package/core/agents/jody.md +150 -0
- package/core/agents/leith.md +111 -0
- package/core/agents/marcelo.md +282 -0
- package/core/agents/melvin.md +101 -0
- package/core/agents/nyx.md +152 -0
- package/core/agents/otto.md +168 -0
- package/core/agents/patricia.md +283 -0
- package/core/commands/design-audit-local.md +155 -0
- package/core/commands/design-audit.md +235 -0
- package/core/commands/design-critique.md +96 -0
- package/core/commands/file-issue.md +22 -0
- package/core/commands/generate-project.md +45 -0
- package/core/commands/implement-issue.md +37 -0
- package/core/commands/implement-project.md +40 -0
- package/core/commands/naturalize.md +61 -0
- package/core/commands/pre-push.md +29 -0
- package/core/commands/prep-review-collect.md +130 -0
- package/core/commands/prep-review-finalize.md +121 -0
- package/core/commands/prep-review-publish.md +113 -0
- package/core/commands/prep-review.md +65 -0
- package/core/commands/project-closeout.md +25 -0
- package/core/skills/agentic-eval/SKILL.md +195 -0
- package/core/skills/chrome-devtools/SKILL.md +97 -0
- package/core/skills/code-review/SKILL.md +26 -0
- package/core/skills/gh-cli/SKILL.md +2202 -0
- package/core/skills/git-commit/SKILL.md +124 -0
- package/core/skills/git-workflow-agents/SKILL.md +462 -0
- package/core/skills/git-workflow-agents/reference.md +220 -0
- package/core/skills/github-actions/SKILL.md +190 -0
- package/core/skills/github-issues/SKILL.md +154 -0
- package/core/skills/llm-structured-outputs/SKILL.md +323 -0
- package/core/skills/llm-structured-outputs/references/provider-details.md +392 -0
- package/core/skills/pre-push/SKILL.md +115 -0
- package/core/skills/refactor/SKILL.md +645 -0
- package/core/skills/web-design-reviewer/SKILL.md +371 -0
- package/core/skills/webapp-testing/SKILL.md +127 -0
- package/core/skills/webapp-testing/test-helper.js +56 -0
- package/core/templates/CLAUDE.md.tmpl +98 -0
- package/core/templates/adr-template.md +67 -0
- package/core/templates/gh-issue-templates/bug.md +39 -0
- package/core/templates/gh-issue-templates/content.md +42 -0
- package/core/templates/gh-issue-templates/enhancement.md +36 -0
- package/core/templates/gh-issue-templates/feature.md +39 -0
- package/core/templates/gh-issue-templates/infrastructure.md +41 -0
- package/core/templates/post-edit-reminders.sh.tmpl +19 -0
- package/core/templates/settings.json.tmpl +90 -0
- package/core/templates/settings.local.json.tmpl +3 -0
- package/core/workflows/agent-implementation-workflow.md +346 -0
- package/core/workflows/generate-project.md +258 -0
- package/core/workflows/implement-project-workflow.md +190 -0
- package/core/workflows/issue-tracking.md +89 -0
- package/core/workflows/project-closeout-ceremony.md +77 -0
- package/core/workflows/review-workflow.md +266 -0
- package/engsys.config.example.yaml +46 -0
- package/install +202 -0
- package/lessons-library/README.md +80 -0
- package/lessons-library/async-callbacks-verify-liveness.md +15 -0
- package/lessons-library/change-isnt-done-until-every-surface-updated.md +15 -0
- package/lessons-library/claim-then-act-for-irreversible-ops.md +16 -0
- package/lessons-library/co-commit-entangled-work.md +15 -0
- package/lessons-library/dependabot-triage-playbook.md +17 -0
- package/lessons-library/deploy-by-digest-and-verify-the-running-revision.md +15 -0
- package/lessons-library/enforce-your-guarantee-at-your-boundary.md +16 -0
- package/lessons-library/gate-changes-on-measurement-not-vibes.md +15 -0
- package/lessons-library/iac-first-no-console-changes.md +15 -0
- package/lessons-library/independent-objective-review-gate.md +15 -0
- package/lessons-library/keep-an-immutable-source-of-truth.md +15 -0
- package/lessons-library/long-agent-runs-checkpoint-not-poll.md +15 -0
- package/lessons-library/model-identity-with-stable-ids-and-provenance.md +15 -0
- package/lessons-library/operator-choices-are-first-class.md +15 -0
- package/lessons-library/prefer-tool-enforced-structured-output.md +15 -0
- package/lessons-library/prove-causation-before-acting.md +15 -0
- package/lessons-library/re-read-state-before-acting.md +14 -0
- package/lessons-library/read-layer-tolerates-unbackfilled-rows.md +15 -0
- package/lessons-library/shell-safety-pipefail-and-validate-before-teardown.md +14 -0
- package/lessons-library/shift-correctness-left-and-distrust-false-greens.md +15 -0
- package/lessons-library/stray-control-bytes-hide-changes.md +14 -0
- package/lessons-library/tests-can-assert-the-bug.md +15 -0
- package/lessons-library/verify-ground-truth-not-reports.md +15 -0
- package/lessons-library/worktrees-need-bootstrap-from-origin-main.md +15 -0
- package/lib/commands.js +356 -0
- package/lib/generate-team-avatars.mjs +251 -0
- package/lib/manifest.js +155 -0
- package/lib/render.js +135 -0
- package/lib/selftest.js +90 -0
- package/lib/util.js +89 -0
- package/lib/yaml.js +156 -0
- package/optional-agents/gary.md +86 -0
- package/optional-agents/jos.md +136 -0
- package/optional-agents/sandy.md +101 -0
- package/optional-agents/steve.md +161 -0
- package/package.json +43 -0
- package/stacks/cloud/aws/claude.fragment.md +17 -0
- package/stacks/cloud/aws/settings.fragment.json +39 -0
- package/stacks/cloud/aws/skills/aws-deployment-preflight/SKILL.md +165 -0
- package/stacks/cloud/aws/skills/cloud-architecture-aws/SKILL.md +265 -0
- package/stacks/cloud/azure/claude.fragment.md +17 -0
- package/stacks/cloud/azure/settings.fragment.json +45 -0
- package/stacks/cloud/azure/skills/azure-deployment-preflight/SKILL.md +175 -0
- package/stacks/cloud/azure/skills/cloud-architecture-azure/SKILL.md +211 -0
- package/stacks/cloud/cloudflare/claude.fragment.md +21 -0
- package/stacks/cloud/cloudflare/settings.fragment.json +31 -0
- package/stacks/cloud/cloudflare/skills/cloud-architecture-cloudflare/SKILL.md +294 -0
- package/stacks/cloud/cloudflare/skills/cloudflare-deployment-preflight/SKILL.md +175 -0
- package/stacks/cloud/gcp/claude.fragment.md +17 -0
- package/stacks/cloud/gcp/settings.fragment.json +40 -0
- package/stacks/cloud/gcp/skills/cloud-architecture-gcp/SKILL.md +208 -0
- package/stacks/cloud/gcp/skills/gcp-deployment-preflight/SKILL.md +137 -0
- package/stacks/db/mongo/skills/mongo-conventions/SKILL.md +96 -0
- package/stacks/db/prisma/claude.fragment.md +49 -0
- package/stacks/db/prisma/skills/docker-database-package-copy/SKILL.md +44 -0
- package/stacks/db/prisma/skills/prisma-conventions/SKILL.md +37 -0
- package/stacks/domain/mobile-growth/skills/apple-ads/SKILL.md +184 -0
- package/stacks/domain/mobile-growth/skills/apple-ads/references/benchmark-notes.md +47 -0
- package/stacks/domain/mobile-growth/skills/apple-ads/references/official-links.md +53 -0
- package/stacks/domain/mobile-growth/skills/google-play-growth/SKILL.md +197 -0
- package/stacks/domain/mobile-growth/skills/google-play-growth/references/benchmark-notes.md +47 -0
- package/stacks/domain/mobile-growth/skills/google-play-growth/references/official-links.md +45 -0
- package/stacks/iac/bicep/claude.fragment.md +14 -0
- package/stacks/iac/bicep/settings.fragment.json +20 -0
- package/stacks/iac/bicep/skills/iac-bicep/SKILL.md +113 -0
- package/stacks/iac/cdk/claude.fragment.md +14 -0
- package/stacks/iac/cdk/settings.fragment.json +23 -0
- package/stacks/iac/cdk/skills/iac-cdk/SKILL.md +104 -0
- package/stacks/iac/terraform/claude.fragment.md +13 -0
- package/stacks/iac/terraform/settings.fragment.json +25 -0
- package/stacks/iac/terraform/skills/iac-terraform/SKILL.md +93 -0
- package/stacks/iac/terraform/skills/terraform-conventions/SKILL.md +87 -0
- package/stacks/lang/kotlin/skills/android-testing/SKILL.md +263 -0
- package/stacks/lang/kotlin/skills/jetpack-compose/SKILL.md +264 -0
- package/stacks/lang/kotlin/skills/kotlin-coroutines/SKILL.md +329 -0
- package/stacks/lang/python/skills/python-conventions/SKILL.md +61 -0
- package/stacks/lang/shell/skills/shell-scripting/SKILL.md +110 -0
- package/stacks/lang/swift/skills/swift-concurrency/SKILL.md +423 -0
- package/stacks/lang/swift/skills/swift-concurrency/references/approachable-concurrency.md +80 -0
- package/stacks/lang/swift/skills/swift-concurrency/references/concurrency-patterns.md +233 -0
- package/stacks/lang/swift/skills/swift-concurrency/references/swiftui-concurrency.md +187 -0
- package/stacks/lang/swift/skills/swift-concurrency/references/synchronization-primitives.md +341 -0
- package/stacks/lang/swift/skills/swift-testing/SKILL.md +497 -0
- package/stacks/lang/swift/skills/swift-testing/references/testing-advanced.md +106 -0
- package/stacks/lang/swift/skills/swift-testing/references/testing-patterns.md +504 -0
- package/stacks/lang/swift/skills/swiftdata/SKILL.md +334 -0
- package/stacks/lang/swift/skills/swiftdata/references/core-data-coexistence.md +504 -0
- package/stacks/lang/swift/skills/swiftdata/references/swiftdata-advanced.md +975 -0
- package/stacks/lang/swift/skills/swiftdata/references/swiftdata-queries.md +675 -0
- package/stacks/lang/swift/skills/swiftui-patterns/SKILL.md +371 -0
- package/stacks/lang/swift/skills/swiftui-patterns/references/architecture-patterns.md +486 -0
- package/stacks/lang/swift/skills/swiftui-patterns/references/deprecated-migration.md +1097 -0
- package/stacks/lang/swift/skills/swiftui-patterns/references/design-polish.md +780 -0
- package/stacks/lang/swift/skills/swiftui-patterns/references/platform-and-sharing.md +696 -0
- package/stacks/lang/typescript/skills/typescript-conventions/SKILL.md +91 -0
- package/stacks/platform/android/claude.fragment.md +40 -0
- package/stacks/platform/android/hooks/pre-push-gradle.sh +70 -0
- package/stacks/platform/android/settings.fragment.json +13 -0
- package/stacks/platform/android/skills/android-build-conventions/SKILL.md +247 -0
- package/stacks/platform/ios/claude.fragment.md +24 -0
- package/stacks/platform/ios/hooks/pre-push-xcodebuild.sh +82 -0
- package/stacks/platform/ios/settings.fragment.json +21 -0
- package/stacks/platform/ios/skills/xcodebuildmcp-simulator-logs/SKILL.md +76 -0
- package/stacks/platform/web/skills/frontend-testing/SKILL.md +246 -0
- package/stacks/platform/web/skills/react-conventions/SKILL.md +261 -0
- package/stacks/platform/web/skills/web-platform-conventions/SKILL.md +55 -0
- package/stacks/tooling/issue-tracker-github/claude.fragment.md +10 -0
- package/stacks/tooling/issue-tracker-github/settings.fragment.json +24 -0
- package/stacks/tooling/issue-tracker-github/skills/issue-tracker-github/SKILL.md +278 -0
- package/stacks/tooling/issue-tracker-linear/claude.fragment.md +17 -0
- package/stacks/tooling/issue-tracker-linear/settings.fragment.json +9 -0
- package/stacks/tooling/issue-tracker-linear/skills/issue-tracker-linear/SKILL.md +183 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# Concurrency Patterns
|
|
2
|
+
|
|
3
|
+
Approachable concurrency patterns introduced in Swift 6.2+ — a philosophy shift where
|
|
4
|
+
code stays single-threaded by default until you choose to introduce concurrency.
|
|
5
|
+
|
|
6
|
+
## Contents
|
|
7
|
+
|
|
8
|
+
- [Core Problem Solved](#core-problem-solved)
|
|
9
|
+
- [SE-0466: Default MainActor Isolation](#se-0466-default-mainactor-isolation)
|
|
10
|
+
- [SE-0461: nonisolated(nonsending)](#se-0461-nonisolatednonsending)
|
|
11
|
+
- [@concurrent Attribute](#concurrent-attribute)
|
|
12
|
+
- [SE-0472: Task.immediate](#se-0472-taskimmediate)
|
|
13
|
+
- [Isolated Conformances](#isolated-conformances)
|
|
14
|
+
- [SE-0481: weak let (Proposed)](#se-0481-weak-let-proposed--swift-62)
|
|
15
|
+
- [SE-0475: Transactional Observation (Observations)](#se-0475-transactional-observation-observations)
|
|
16
|
+
- [Global and Static State](#global-and-static-state)
|
|
17
|
+
- [Migration and Build Settings](#migration-and-build-settings)
|
|
18
|
+
- [Summary](#summary)
|
|
19
|
+
|
|
20
|
+
## Core Problem Solved
|
|
21
|
+
|
|
22
|
+
In Swift 6.0/6.1, data-race safety was enforced at compile time, but the most
|
|
23
|
+
natural code to write often produced data-race errors. Async functions on types
|
|
24
|
+
with mutable state would implicitly hop to the global concurrent executor,
|
|
25
|
+
causing send-safety violations even when no actual parallelism was intended.
|
|
26
|
+
|
|
27
|
+
```swift
|
|
28
|
+
// Swift 6.0/6.1: This produces a data-race error
|
|
29
|
+
class PhotoProcessor {
|
|
30
|
+
func extractSticker(data: Data, with id: String?) async -> Sticker? { /* ... */ }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@MainActor
|
|
34
|
+
final class StickerModel {
|
|
35
|
+
let photoProcessor = PhotoProcessor()
|
|
36
|
+
|
|
37
|
+
func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? {
|
|
38
|
+
guard let data = try await item.loadTransferable(type: Data.self) else { return nil }
|
|
39
|
+
// Error: Sending 'self.photoProcessor' risks causing data races
|
|
40
|
+
return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```swift
|
|
46
|
+
// Swift 6.2: The same code compiles without error
|
|
47
|
+
// because extractSticker stays on the caller's actor
|
|
48
|
+
class PhotoProcessor {
|
|
49
|
+
func extractSticker(data: Data, with id: String?) async -> Sticker? { /* ... */ }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@MainActor
|
|
53
|
+
final class StickerModel {
|
|
54
|
+
let photoProcessor = PhotoProcessor()
|
|
55
|
+
|
|
56
|
+
func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? {
|
|
57
|
+
guard let data = try await item.loadTransferable(type: Data.self) else { return nil }
|
|
58
|
+
return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## SE-0466: Default MainActor Isolation
|
|
64
|
+
|
|
65
|
+
Enable with the `-default-isolation MainActor` compiler flag or the Xcode 26
|
|
66
|
+
"Approachable Concurrency" build setting.
|
|
67
|
+
|
|
68
|
+
**What it does:**
|
|
69
|
+
- All declarations in the module are implicitly `@MainActor` unless opted out.
|
|
70
|
+
- Global and static variables are protected by the main actor by default.
|
|
71
|
+
- Protocol conformances are implicitly isolated to `@MainActor`.
|
|
72
|
+
- Eliminates most annotation burden for single-threaded UI code.
|
|
73
|
+
|
|
74
|
+
**Recommended for:** Apps, scripts, and executable targets. Not recommended for
|
|
75
|
+
library targets that should remain actor-agnostic.
|
|
76
|
+
|
|
77
|
+
```swift
|
|
78
|
+
// With default MainActor isolation -- no @MainActor annotations needed:
|
|
79
|
+
final class StickerLibrary {
|
|
80
|
+
static let shared = StickerLibrary()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
final class StickerModel {
|
|
84
|
+
let photoProcessor = PhotoProcessor()
|
|
85
|
+
var selection: [PhotosPickerItem] = []
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
extension StickerModel: Exportable {
|
|
89
|
+
func export() { photoProcessor.exportAsPNG() }
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## SE-0461: nonisolated(nonsending)
|
|
94
|
+
|
|
95
|
+
Nonisolated async functions stay on the caller's actor by default instead of
|
|
96
|
+
hopping to the global concurrent executor. This is the `nonisolated(nonsending)`
|
|
97
|
+
default behavior.
|
|
98
|
+
|
|
99
|
+
**Key implication:** Values passed into an async function are never sent outside
|
|
100
|
+
the actor, eliminating data races without annotation.
|
|
101
|
+
|
|
102
|
+
To explicitly opt into background execution, use `@concurrent`.
|
|
103
|
+
|
|
104
|
+
## @concurrent Attribute
|
|
105
|
+
|
|
106
|
+
Ensures a function always runs on the concurrent thread pool, freeing the
|
|
107
|
+
calling actor for other work.
|
|
108
|
+
|
|
109
|
+
```swift
|
|
110
|
+
class PhotoProcessor {
|
|
111
|
+
var cachedStickers: [String: Sticker] = [:]
|
|
112
|
+
|
|
113
|
+
func extractSticker(data: Data, with id: String) async -> Sticker {
|
|
114
|
+
if let sticker = cachedStickers[id] { return sticker }
|
|
115
|
+
let sticker = await Self.extractSubject(from: data)
|
|
116
|
+
cachedStickers[id] = sticker
|
|
117
|
+
return sticker
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@concurrent
|
|
121
|
+
static func extractSubject(from data: Data) async -> Sticker { /* ... */ }
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Steps to offload a function to background:**
|
|
126
|
+
1. Ensure the containing type is `nonisolated` (or the function itself).
|
|
127
|
+
2. Add `@concurrent` to the function.
|
|
128
|
+
3. Add `async` if not already asynchronous.
|
|
129
|
+
4. Add `await` at call sites.
|
|
130
|
+
|
|
131
|
+
```swift
|
|
132
|
+
nonisolated struct PhotoProcessor {
|
|
133
|
+
@concurrent
|
|
134
|
+
func process(data: Data) async -> ProcessedPhoto? { /* ... */ }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
processedPhotos[item.id] = await PhotoProcessor().process(data: data)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## SE-0472: Task.immediate
|
|
141
|
+
|
|
142
|
+
`Task.immediate` starts executing synchronously on the current actor before any
|
|
143
|
+
suspension point, rather than being enqueued. There is also
|
|
144
|
+
`Task.immediateDetached` which combines immediate start with detached semantics.
|
|
145
|
+
|
|
146
|
+
```swift
|
|
147
|
+
Task.immediate { await handleUserInput() }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Use for latency-sensitive work where enqueue delay is unacceptable.
|
|
151
|
+
|
|
152
|
+
## Isolated Conformances
|
|
153
|
+
|
|
154
|
+
A conformance that needs MainActor state is called an *isolated conformance*.
|
|
155
|
+
The compiler ensures the conformance is only used in a matching isolation
|
|
156
|
+
context.
|
|
157
|
+
|
|
158
|
+
```swift
|
|
159
|
+
protocol Exportable {
|
|
160
|
+
func export()
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
extension StickerModel: @MainActor Exportable {
|
|
164
|
+
func export() { photoProcessor.exportAsPNG() }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
@MainActor
|
|
168
|
+
struct ImageExporter {
|
|
169
|
+
var items: [any Exportable]
|
|
170
|
+
|
|
171
|
+
mutating func add(_ item: StickerModel) {
|
|
172
|
+
items.append(item) // OK -- on MainActor
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// But in a nonisolated context:
|
|
177
|
+
nonisolated struct GenericExporter {
|
|
178
|
+
var items: [any Exportable]
|
|
179
|
+
|
|
180
|
+
mutating func add(_ item: StickerModel) {
|
|
181
|
+
// Error: Main actor-isolated conformance of 'StickerModel' to
|
|
182
|
+
// 'Exportable' cannot be used in nonisolated context
|
|
183
|
+
items.append(item)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## SE-0481: weak let (Proposed — Swift 6.2+)
|
|
189
|
+
|
|
190
|
+
Immutable weak references (`weak let`) enable `Sendable` conformance for types
|
|
191
|
+
that hold weak references, since immutability guarantees thread safety. Proposed
|
|
192
|
+
in SE-0481; may not yet be available in shipping toolchains.
|
|
193
|
+
|
|
194
|
+
## SE-0475: Transactional Observation (Observations)
|
|
195
|
+
|
|
196
|
+
`Observations { }` provides transactional observation of `@Observable` types
|
|
197
|
+
via `AsyncSequence`.
|
|
198
|
+
|
|
199
|
+
```swift
|
|
200
|
+
for await _ in Observations { model.count } {
|
|
201
|
+
print("Count changed to \(model.count)")
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Global and Static State
|
|
206
|
+
|
|
207
|
+
Global and static variables are prone to data races. The most common protection
|
|
208
|
+
is `@MainActor`:
|
|
209
|
+
|
|
210
|
+
```swift
|
|
211
|
+
@MainActor
|
|
212
|
+
final class StickerLibrary {
|
|
213
|
+
static let shared = StickerLibrary() // protected by MainActor
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
With default MainActor isolation (SE-0466), this annotation is implicit.
|
|
218
|
+
|
|
219
|
+
## Migration and Build Settings
|
|
220
|
+
|
|
221
|
+
All approachable concurrency features are opt-in via:
|
|
222
|
+
- **Xcode 26:** Swift Compiler > Concurrency section in build settings.
|
|
223
|
+
- **SwiftPM:** `swiftSettings` in Package.swift using the `SwiftSetting` API.
|
|
224
|
+
|
|
225
|
+
Swift 6.2 includes migration tooling to help make necessary code changes
|
|
226
|
+
automatically. See swift.org/migration for details.
|
|
227
|
+
|
|
228
|
+
## Summary
|
|
229
|
+
|
|
230
|
+
The Swift 6.2 concurrency progression:
|
|
231
|
+
1. Start with code that runs on the main actor by default (no data race risk).
|
|
232
|
+
2. Async functions run wherever they are called from (still no data race risk).
|
|
233
|
+
3. When you need performance, offload specific code with `@concurrent`.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# SwiftUI Concurrency Guide
|
|
2
|
+
|
|
3
|
+
Concurrency patterns and best practices specific to SwiftUI applications.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- [MainActor Default in SwiftUI](#mainactor-default-in-swiftui)
|
|
8
|
+
- [Where SwiftUI Runs Code Off the Main Thread](#where-swiftui-runs-code-off-the-main-thread)
|
|
9
|
+
- [Sendable Closures and Data-Race Safety](#sendable-closures-and-data-race-safety)
|
|
10
|
+
- [Structuring Async Work](#structuring-async-work)
|
|
11
|
+
- [The .task Modifier](#the-task-modifier)
|
|
12
|
+
- [@Observable View Models](#observable-view-models)
|
|
13
|
+
- [Async Observation with Observations (SE-0475)](#async-observation-with-observations-se-0475)
|
|
14
|
+
- [Performance-Driven Concurrency](#performance-driven-concurrency)
|
|
15
|
+
- [Common SwiftUI Concurrency Mistakes](#common-swiftui-concurrency-mistakes)
|
|
16
|
+
|
|
17
|
+
## MainActor Default in SwiftUI
|
|
18
|
+
|
|
19
|
+
- `View` is `@MainActor` isolated by default; `body` and all members inherit
|
|
20
|
+
this isolation.
|
|
21
|
+
- Swift 6.2 can infer `@MainActor` for all types in a module via default actor
|
|
22
|
+
isolation (SE-0466).
|
|
23
|
+
- This default aligns with UIKit/AppKit `@MainActor` APIs and simplifies UI
|
|
24
|
+
code.
|
|
25
|
+
|
|
26
|
+
## Where SwiftUI Runs Code Off the Main Thread
|
|
27
|
+
|
|
28
|
+
SwiftUI may evaluate some view logic on background threads for performance:
|
|
29
|
+
|
|
30
|
+
- `Shape` path generation
|
|
31
|
+
- `Layout` methods (`sizeThatFits`, `placeSubviews`)
|
|
32
|
+
- `visualEffect` closures
|
|
33
|
+
- `onGeometryChange` closures
|
|
34
|
+
|
|
35
|
+
These APIs often require `Sendable` closures to reflect their off-main-thread
|
|
36
|
+
runtime semantics.
|
|
37
|
+
|
|
38
|
+
## Sendable Closures and Data-Race Safety
|
|
39
|
+
|
|
40
|
+
Accessing `@MainActor` state from a `Sendable` closure is unsafe and flagged by
|
|
41
|
+
the compiler.
|
|
42
|
+
|
|
43
|
+
**Fix:** Capture value copies in the closure capture list.
|
|
44
|
+
|
|
45
|
+
```swift
|
|
46
|
+
// WRONG: Captures @MainActor state directly
|
|
47
|
+
.visualEffect { content, proxy in
|
|
48
|
+
content.offset(y: self.offset) // Error: @MainActor state in Sendable closure
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// CORRECT: Capture a copy
|
|
52
|
+
let currentOffset = offset
|
|
53
|
+
// ... use in closure:
|
|
54
|
+
.visualEffect { [currentOffset] content, proxy in
|
|
55
|
+
content.offset(y: currentOffset)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Avoid sending `self` into a `Sendable` closure just to read a single property.
|
|
60
|
+
|
|
61
|
+
## Structuring Async Work
|
|
62
|
+
|
|
63
|
+
SwiftUI action callbacks are synchronous so UI updates (like loading states) can
|
|
64
|
+
be immediate.
|
|
65
|
+
|
|
66
|
+
```swift
|
|
67
|
+
struct ContentView: View {
|
|
68
|
+
@State private var isLoading = false
|
|
69
|
+
@State private var result: String?
|
|
70
|
+
|
|
71
|
+
var body: some View {
|
|
72
|
+
Button("Load") {
|
|
73
|
+
isLoading = true // Immediate UI update
|
|
74
|
+
Task {
|
|
75
|
+
result = await fetchData()
|
|
76
|
+
isLoading = false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Pattern:** Use state as the boundary. Async work updates model/state; UI
|
|
84
|
+
reacts synchronously.
|
|
85
|
+
|
|
86
|
+
## The .task Modifier
|
|
87
|
+
|
|
88
|
+
Prefer `.task` over manual `Task` creation in views:
|
|
89
|
+
|
|
90
|
+
```swift
|
|
91
|
+
.task {
|
|
92
|
+
await loadInitialData()
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Advantages:**
|
|
97
|
+
- Automatically cancels on view disappear.
|
|
98
|
+
- Inherits the view's actor isolation (`@MainActor`).
|
|
99
|
+
- No need to store `Task` references for cancellation.
|
|
100
|
+
|
|
101
|
+
Use `.task(id:)` to restart work when a value changes:
|
|
102
|
+
|
|
103
|
+
```swift
|
|
104
|
+
.task(id: selectedItem) {
|
|
105
|
+
details = await fetchDetails(for: selectedItem)
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## @Observable View Models
|
|
110
|
+
|
|
111
|
+
- Annotate view models with both `@Observable` and `@MainActor`.
|
|
112
|
+
- Use `@State` to own an `@Observable` instance (replaces `@StateObject`).
|
|
113
|
+
- Avoid `@ObservedObject` / `@StateObject` / `ObservableObject` in new code.
|
|
114
|
+
|
|
115
|
+
```swift
|
|
116
|
+
@Observable @MainActor
|
|
117
|
+
final class ViewModel {
|
|
118
|
+
var items: [Item] = []
|
|
119
|
+
var isLoading = false
|
|
120
|
+
|
|
121
|
+
func load() async {
|
|
122
|
+
isLoading = true
|
|
123
|
+
items = await fetchItems()
|
|
124
|
+
isLoading = false
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
struct ItemListView: View {
|
|
129
|
+
@State private var viewModel = ViewModel()
|
|
130
|
+
|
|
131
|
+
var body: some View {
|
|
132
|
+
List(viewModel.items) { item in
|
|
133
|
+
Text(item.name)
|
|
134
|
+
}
|
|
135
|
+
.task { await viewModel.load() }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Async Observation with Observations (SE-0475)
|
|
141
|
+
|
|
142
|
+
Use `Observations { }` for transactional async observation:
|
|
143
|
+
|
|
144
|
+
```swift
|
|
145
|
+
.task {
|
|
146
|
+
for await _ in Observations { viewModel.searchText } {
|
|
147
|
+
await viewModel.performSearch()
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Performance-Driven Concurrency
|
|
153
|
+
|
|
154
|
+
- Offload expensive work from the main actor to avoid hitches.
|
|
155
|
+
- Keep time-sensitive UI logic (animations, gesture responses) synchronous.
|
|
156
|
+
- Separate UI code from long-running async work.
|
|
157
|
+
|
|
158
|
+
```swift
|
|
159
|
+
@Observable @MainActor
|
|
160
|
+
final class ImageProcessor {
|
|
161
|
+
var processedImage: UIImage?
|
|
162
|
+
|
|
163
|
+
func process(data: Data) async {
|
|
164
|
+
// Offload heavy work
|
|
165
|
+
let result = await Self.runProcessing(data: data)
|
|
166
|
+
processedImage = result
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@concurrent
|
|
170
|
+
nonisolated static func runProcessing(data: Data) async -> UIImage {
|
|
171
|
+
// Runs on background thread pool
|
|
172
|
+
// ...
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Common SwiftUI Concurrency Mistakes
|
|
178
|
+
|
|
179
|
+
1. **Creating `Task` in `body`.** Use `.task` modifier instead.
|
|
180
|
+
2. **Not cancelling tasks.** `.task` does this automatically; manual `Task`
|
|
181
|
+
references must be cancelled in `onDisappear`.
|
|
182
|
+
3. **Blocking MainActor in view updates.** Move heavy computation to
|
|
183
|
+
`@concurrent` functions.
|
|
184
|
+
4. **Using `Task.detached` in views.** Loses actor context. Use `Task { }` or
|
|
185
|
+
`.task` modifier.
|
|
186
|
+
5. **Updating state from background.** Always update `@State` / `@Observable`
|
|
187
|
+
properties on `@MainActor`.
|