Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Swift 6.2 Approachable Concurrency — single-threaded by default, @concurrent for explicit background offloading, isolated conformances for main actor types.
.claude/skills/loulanyue-swift-concurrency-6-2/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 39% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 19% | 0% |
| case-05 | ✗→✓ | ▲ Improved | 41% | 0% |
| case-06 | ✗→✓ | ▲ Improved | -12% | 0% |
| case-10 | ✗→✓ | ▲ Improved | -22% | 0% |
Patterns for adopting Swift 6.2's concurrency model where code runs single-threaded by default and concurrency is introduced explicitly. Eliminates common data-race errors without sacrificing performance.
In Swift 6.1 and earlier, async functions could be implicitly offloaded to background threads, causing data-race errors even in seemingly safe code:
swift// Swift 6.1: ERROR @MainActor final class StickerModel { let photoProcessor = PhotoProcessor() func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? { guard let data = try await item.loadTransferable(type: Data.self) else { return nil } // Error: Sending 'self.photoProcessor' risks causing data races return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier) } }
Swift 6.2 fixes this: async functions stay on the calling actor by default.
swift// Swift 6.2: OK — async stays on MainActor, no data race @MainActor final class StickerModel { let photoProcessor = PhotoProcessor() func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? { guard let data = try await item.loadTransferable(type: Data.self) else { return nil } return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier) } }
MainActor types can now conform to non-isolated protocols safely:
swiftprotocol Exportable { func export() } // Swift 6.1: ERROR — crosses into main actor-isolated code // Swift 6.2: OK with isolated conformance extension StickerModel: @MainActor Exportable { func export() { photoProcessor.exportAsPNG() } }
The compiler ensures the conformance is only used on the main actor:
swift// OK — ImageExporter is also @MainActor @MainActor struct ImageExporter { var items: [any Exportable] mutating func add(_ item: StickerModel) { items.append(item) // Safe: same actor isolation } } // ERROR — nonisolated context can't use MainActor conformance nonisolated struct ImageExporter { var items: [any Exportable] mutating func add(_ item: StickerModel) { items.append(item) // Error: Main actor-isolated conformance cannot be used here } }
Protect global/static state with MainActor:
swift// Swift 6.1: ERROR — non-Sendable type may have shared mutable state final class StickerLibrary { static let shared: StickerLibrary = .init() // Error } // Fix: Annotate with @MainActor @MainActor final class StickerLibrary { static let shared: StickerLibrary = .init() // OK }
Swift 6.2 introduces a mode where MainActor is inferred by default — no manual annotations needed:
swift// With MainActor default inference enabled: final class StickerLibrary { static let shared: StickerLibrary = .init() // Implicitly @MainActor } final class StickerModel { let photoProcessor: PhotoProcessor var selection: [PhotosPickerItem] // Implicitly @MainActor } extension StickerModel: Exportable { // Implicitly @MainActor conformance func export() { photoProcessor.exportAsPNG() } }
This mode is opt-in and recommended for apps, scripts, and other executable targets.
When you need actual parallelism, explicitly offload with @concurrent:
> Important: This example requires Approachable Concurrency build settings — SE-0466 (MainActor default isolation) and SE-0461 (NonisolatedNonsendingByDefault). With these enabled, extractSticker stays on the caller's actor, making mutable state access safe. Without these settings, this code has a data race — the compiler will flag it.
swiftnonisolated final class PhotoProcessor { private var cachedStickers: [String: Sticker] = [:] func extractSticker(data: Data, with id: String) async -> Sticker { if let sticker = cachedStickers[id] { return sticker } let sticker = await Self.extractSubject(from: data) cachedStickers[id] = sticker return sticker } // Offload expensive work to concurrent thread pool @concurrent static func extractSubject(from data: Data) async -> Sticker { /* ... */ } } // Callers must await let processor = PhotoProcessor() processedPhotos[item.id] = await processor.extractSticker(data: data, with: item.id)
To use @concurrent:
nonisolated@concurrent to the functionasync if not already asynchronousawait at call sites| Decision | Rationale | |----------|-----------| | Single-threaded by default | Most natural code is data-race free; concurrency is opt-in | | Async stays on calling actor | Eliminates implicit offloading that caused data-race errors | | Isolated conformances | MainActor types can conform to protocols without unsafe workarounds | | @concurrent explicit opt-in | Background execution is a deliberate performance choice, not accidental | | MainActor default inference | Reduces boilerplate @MainActor annotations for app targets | | Opt-in adoption | Non-breaking migration path — enable features incrementally |
SwiftSettings API in package manifest@concurrent where needed: Profile first, then offload hot paths@concurrent only for CPU-intensive work — image processing, compression, complex computationnonisolated workarounds or @Sendable wrappers@concurrent to every async function (most don't need background execution)nonisolated to suppress compiler errors without understanding isolationDispatchQueue patterns when actors provide the same safetymodel.availability checks in concurrency-related Foundation Models code| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 14,535 | 8,083 | -44% | 1 | 1 | 0% | 2,430 | 3,371 | +39% | 0 | 0 | — |
case-02 | fail→fail | 21,993 | 14,872 | -32% | 1 | 1 | 0% | 4,055 | 4,596 | +13% | 0 | 0 | — |
case-03 | fail→fail | 21,586 | 14,767 | -32% | 1 | 1 | 0% | 3,419 | 4,568 | +34% | 0 | 0 | — |
case-04 | fail→pass | 12,006 | 3,499 | -71% | 1 | 1 | 0% | 2,067 | 2,455 | +19% | 0 | 0 | — |
case-05 | fail→pass | 13,155 | 6,737 | -49% | 1 | 1 | 0% | 2,206 | 3,108 | +41% | 0 | 0 | — |
case-06 | fail→pass | 16,094 | 2,813 | -83% | 1 | 1 | 0% | 2,636 | 2,317 | -12% | 0 | 0 | — |
case-07 | pass→pass | 9,340 | 4,376 | -53% | 1 | 1 | 0% | 1,602 | 2,671 | +67% | 0 | 0 | — |
case-08 | pass→pass | 6,706 | 1,751 | -74% | 1 | 1 | 0% | 933 | 2,179 | +134% | 0 | 0 | — |
case-09 | pass→pass | 8,614 | 2,751 | -68% | 1 | 1 | 0% | 1,264 | 2,317 | +83% | 0 | 0 | — |
case-10 | fail→pass | 21,669 | 5,247 | -76% | 1 | 1 | 0% | 3,533 | 2,764 | -22% | 0 | 0 | — |
case-11 | fail→pass | 13,255 | 5,118 | -61% | 1 | 1 | 0% | 2,107 | 2,793 | +33% | 0 | 0 | — |
case-12 | pass→pass | 13,483 | 6,159 | -54% | 1 | 1 | 0% | 1,979 | 2,822 | +43% | 0 | 0 | — |
case-13 | pass→pass | 14,186 | 7,321 | -48% | 1 | 1 | 0% | 2,298 | 3,166 | +38% | 0 | 0 | — |
case-14 | fail→pass | 17,762 | 6,171 | -65% | 1 | 1 | 0% | 2,878 | 2,916 | +1% | 0 | 0 | — |
case-15 | pass→pass | 8,161 | 3,876 | -53% | 1 | 1 | 0% | 1,200 | 2,491 | +108% | 0 | 0 | — |
case-16 | fail→pass | 10,530 | 2,968 | -72% | 1 | 1 | 0% | 1,588 | 2,418 | +52% | 0 | 0 | — |
case-17 | pass→pass | 6,844 | 1,991 | -71% | 1 | 1 | 0% | 1,052 | 2,209 | +110% | 0 | 0 | — |
case-18 | pass→pass | 15,873 | 7,079 | -55% | 1 | 1 | 0% | 2,185 | 2,946 | +35% | 0 | 0 | — |
case-19 | pass→pass | 11,599 | 3,321 | -71% | 1 | 1 | 0% | 1,974 | 2,490 | +26% | 0 | 0 | — |
case-20 | pass→pass | 12,738 | 7,496 | -41% | 1 | 1 | 0% | 2,487 | 3,204 | +29% | 0 | 0 | — |
case-21 | pass→pass | 11,608 | 7,693 | -34% | 1 | 1 | 0% | 2,171 | 3,239 | +49% | 0 | 0 | — |
case-22 | pass→fail | 14,355 | 11,470 | -20% | 1 | 1 | 0% | 2,630 | 4,066 | +55% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +32 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.