swift-concurrency-6-2
Implements Swift 6.2 concurrency, ensuring code stays single-threaded by default and offloading tasks explicitly.
Install
mkdir -p .claude/skills/swift-concurrency-6-2 && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/11786" && unzip -o skill.zip -d .claude/skills/swift-concurrency-6-2 && rm skill.zipInstalls to .claude/skills/swift-concurrency-6-2
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Swift 6.2 Approachable Concurrency — single-threaded by default, @concurrentKey capabilities
- →Migrate Swift 5.x or 6.0/6.1 projects to Swift 6.2 concurrency model
- →Resolve data-race safety compiler errors by enforcing single-threaded defaults
- →Design MainActor-based app architecture
- →Implement protocol conformances on MainActor-isolated types
- →Offload CPU-intensive work to background threads using `@concurrent`
- →Enable Approachable Concurrency build settings in Xcode 26
How it works
The skill provides patterns for adopting Swift 6.2's concurrency model, where code runs single-threaded by default and concurrency is introduced explicitly. It addresses implicit background offloading, isolated conformances, and global/static variables.
Inputs & outputs
When to use swift-concurrency-6-2
- →Migrating legacy Swift 5.x code to Swift 6.2
- →Resolving compiler-level data-race safety errors
- →Designing apps using MainActor-based architecture
- →Offloading CPU-intensive tasks to background threads
About this skill
Swift 6.2 Approachable Concurrency
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.
When to Activate
- Migrating Swift 5.x or 6.0/6.1 projects to Swift 6.2
- Resolving data-race safety compiler errors
- Designing MainActor-based app architecture
- Offloading CPU-intensive work to background threads
- Implementing protocol conformances on MainActor-isolated types
- Enabling Approachable Concurrency build settings in Xcode 26
Core Problem: Implicit Background Offloading
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 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 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)
}
}
Core Pattern — Isolated Conformances
MainActor types can now conform to non-isolated protocols safely:
protocol 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:
// 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
}
}
Core Pattern — Global and Static Variables
Protect global/static state with MainActor:
// 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
}
MainActor Default Inference Mode
Swift 6.2 introduces a mode where MainActor is inferred by default — no manual annotations needed:
// 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.
Core Pattern — @concurrent for Background Work
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,
extractStickerstays on the caller's actor, making mutable state access safe. Without these settings, this code has a data race — the compiler will flag it.
nonisolated 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:
- Mark the containing type as
nonisolated - Add
@concurrentto the function - Add
asyncif not already asynchronous - Add
awaitat call sites
Key Design Decisions
| 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 |
Migration Steps
- Enable in Xcode: Swift Compiler > Concurrency section in Build Settings
- Enable in SPM: Use
SwiftSettingsAPI in package manifest - Use migration tooling: Automatic code changes via swift.org/migration
- Start with MainActor defaults: Enable inference mode for app targets
- Add
@concurrentwhere needed: Profile first, then offload hot paths - Test thoroughly: Data-race issues become compile-time errors
Best Practices
- Start on MainActor — write single-threaded code first, optimize later
- Use
@concurrentonly for CPU-intensive work — image processing, compression, complex computation - Enable MainActor inference mode for app targets that are mostly single-threaded
- Profile before offloading — use Instruments to find actual bottlenecks
- Protect globals with MainActor — global/static mutable state needs actor isolation
- Use isolated conformances instead of
nonisolatedworkarounds or@Sendablewrappers - Migrate incrementally — enable features one at a time in build settings
Anti-Patterns to Avoid
- Applying
@concurrentto every async function (most don't need background execution) - Using
nonisolatedto suppress compiler errors without understanding isolation - Keeping legacy
DispatchQueuepatterns when actors provide the same safety - Skipping
model.availabilitychecks in concurrency-related Foundation Models code - Fighting the compiler — if it reports a data race, the code has a real concurrency issue
- Assuming all async code runs in the background (Swift 6.2 default: stays on calling actor)
When to Use
- All new Swift 6.2+ projects (Approachable Concurrency is the recommended default)
- Migrating existing apps from Swift 5.x or 6.0/6.1 concurrency
- Resolving data-race safety compiler errors during Xcode 26 adoption
- Building MainActor-centric app architectures (most UI apps)
- Performance optimization — offloading specific heavy computations to background
When not to use it
- →When applying `@concurrent` to every async function
- →When using `nonisolated` to suppress compiler errors without understanding isolation
- →When keeping legacy `DispatchQueue` patterns instead of actors
Limitations
- →Requires Approachable Concurrency build settings for `@concurrent` to prevent data races
- →MainActor default inference mode is opt-in
- →Global/static mutable state needs actor isolation
How it compares
This skill guides the adoption of Swift 6.2's approachable concurrency model, which defaults to single-threaded execution and requires explicit opt-in for parallelism, differing from previous Swift versions where implicit offloading could l
Compared to similar skills
swift-concurrency-6-2 side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| swift-concurrency-6-2 (this skill) | 0 | 2mo | No flags | Advanced |
| swiftui-patterns-developer | 1 | 6mo | No flags | Intermediate |
| swift-concurrency | 4 | 2mo | No flags | Advanced |
| migrating-to-tuist-generated-projects | 3 | 3mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by JantonioFC
View all by JantonioFC →You might also like
swiftui-patterns-developer
anyproto
SwiftUI view structure, composition, and best practices. Use when refactoring SwiftUI views, organizing view files, or extracting subviews.
swift-concurrency
AvdLee
Expert guidance on Swift Concurrency best practices, patterns, and implementation. Use when developers mention: (1) Swift Concurrency, async/await, actors, or tasks, (2) "use Swift Concurrency" or "modern concurrency patterns", (3) migrating to Swift 6, (4) data races or thread safety issues, (5) refactoring closures to async/await, (6) @MainActor, Sendable, or actor isolation, (7) concurrent code architecture or performance optimization, (8) concurrency-related linter warnings (SwiftLint or similar; e.g. async_without_await, Sendable/actor isolation/MainActor lint).
migrating-to-tuist-generated-projects
tuist
Migrates existing Xcode projects to Tuist generated workspaces with build and run validation, external dependency mapping, and migration checklists. Use when adopting Tuist for an existing app or converting a hand-edited Xcode project to generated projects.
implement-feature
tddworks
Guide for implementing features in ClaudeBar following architecture-first design, TDD, rich domain models, and Swift 6.2 patterns. Use this skill when: (1) Adding new functionality to the app (2) Creating domain models that follow user's mental model (3) Building SwiftUI views that consume domain models directly (4) User asks "how do I implement X" or "add feature Y" (5) Implementing any feature that spans Domain, Infrastructure, and App layers
feature-toggle-developer
anyproto
Guides systematic removal of feature toggles from the codebase with automated cleanup detection. Use when removing feature flags, enabling toggles permanently, or cleaning up unused code after toggle removal.
add-provider
tddworks
Guide for adding new AI providers to ClaudeBar using TDD patterns. Use this skill when: (1) Adding a new AI assistant provider (like Antigravity, Cursor, etc.) (2) Creating a usage probe for a CLI tool or local API (3) Following TDD to implement provider integration (4) User asks "how do I add a new provider" or "create a provider for X"