Marketplace swift-concurrency
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.
git clone https://github.com/aiskillstore/marketplace
T=$(mktemp -d) && git clone --depth=1 https://github.com/aiskillstore/marketplace "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/avdlee/swift-concurrency" ~/.claude/skills/aiskillstore-marketplace-swift-concurrency && rm -rf "$T"
skills/avdlee/swift-concurrency/SKILL.mdSwift Concurrency
Overview
This skill provides expert guidance on Swift Concurrency, covering modern async/await patterns, actors, tasks, Sendable conformance, and migration to Swift 6. Use this skill to help developers write safe, performant concurrent code and navigate the complexities of Swift's structured concurrency model.
Agent Behavior Contract (Follow These Rules)
- Analyze the project/package file to find out which Swift language mode (Swift 5.x vs Swift 6) and which Xcode/Swift toolchain is used when advice depends on it.
- Before proposing fixes, identify the isolation boundary:
, custom actor, actor instance isolation, or nonisolated.@MainActor - Do not recommend
as a blanket fix. Justify why main-actor isolation is correct for the code.@MainActor - Prefer structured concurrency (child tasks, task groups) over unstructured tasks. Use
only with a clear reason.Task.detached - If recommending
,@preconcurrency
, or@unchecked Sendable
, require:nonisolated(unsafe)- a documented safety invariant
- a follow-up ticket to remove or migrate it
- For migration work, optimize for minimal blast radius (small, reviewable changes) and add verification steps.
- Course references are for deeper learning only. Use them sparingly and only when they clearly help answer the developer’s question.
Project Settings Intake (Evaluate Before Advising)
Concurrency behavior depends on build settings. Always try to determine:
- Default actor isolation (is the module default
or@MainActor
?)nonisolated - Strict concurrency checking level (minimal/targeted/complete)
- Whether upcoming features are enabled (especially
)NonisolatedNonsendingByDefault - Swift language mode (Swift 5.x vs Swift 6) and SwiftPM tools version
Manual checks (no scripts)
- SwiftPM:
- Check
forPackage.swift
..defaultIsolation(MainActor.self) - Check
forPackage.swift
..enableUpcomingFeature("NonisolatedNonsendingByDefault") - Check for strict concurrency flags:
(or similar)..enableExperimentalFeature("StrictConcurrency=targeted") - Check tools version at the top:
// swift-tools-version: ...
- Check
- Xcode projects:
- Search
for:project.pbxprojSWIFT_DEFAULT_ACTOR_ISOLATIONSWIFT_STRICT_CONCURRENCY
(and/orSWIFT_UPCOMING_FEATURE_
)SWIFT_ENABLE_EXPERIMENTAL_FEATURES
- Search
If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance.
Quick Decision Tree
When a developer needs concurrency guidance, follow this decision tree:
-
Starting fresh with async code?
- Read
for foundational patternsreferences/async-await-basics.md - For parallel operations →
(async let, task groups)references/tasks.md
- Read
-
Protecting shared mutable state?
- Need to protect class-based state →
(actors, @MainActor)references/actors.md - Need thread-safe value passing →
(Sendable conformance)references/sendable.md
- Need to protect class-based state →
-
Managing async operations?
- Structured async work →
(Task, child tasks, cancellation)references/tasks.md - Streaming data →
(AsyncSequence, AsyncStream)references/async-sequences.md
- Structured async work →
-
Working with legacy frameworks?
- Core Data integration →
references/core-data.md - General migration →
references/migration.md
- Core Data integration →
-
Performance or debugging issues?
- Slow async code →
(profiling, suspension points)references/performance.md - Testing concerns →
(XCTest, Swift Testing)references/testing.md
- Slow async code →
-
Understanding threading behavior?
- Read
for thread/task relationship and isolationreferences/threading.md
- Read
-
Memory issues with tasks?
- Read
for retain cycle preventionreferences/memory-management.md
- Read
Triage-First Playbook (Common Errors -> Next Best Move)
- "Sending value of non-Sendable type ... risks causing data races"
- First: identify where the value crosses an isolation boundary
- Then: use
andreferences/sendable.md
(especially Swift 6.2 behavior changes)references/threading.md
- "Main actor-isolated ... cannot be used from a nonisolated context"
- First: decide if it truly belongs on
@MainActor - Then: use
(global actors,references/actors.md
, isolated parameters) andnonisolated
(default isolation)references/threading.md
- First: decide if it truly belongs on
- "Class property 'current' is unavailable from asynchronous contexts" (Thread APIs)
- Use
to avoid thread-centric debugging and rely on isolation + Instrumentsreferences/threading.md
- Use
- XCTest async errors like "wait(...) is unavailable from asynchronous contexts"
- Use
(references/testing.md
and Swift Testing patterns)await fulfillment(of:)
- Use
- Core Data concurrency warnings/errors
- Use
(DAO/references/core-data.md
, default isolation conflicts)NSManagedObjectID
- Use
Core Patterns Reference
When to Use Each Concurrency Tool
async/await - Making existing synchronous code asynchronous
// Use for: Single asynchronous operations func fetchUser() async throws -> User { try await networkClient.get("/user") }
async let - Running multiple independent async operations in parallel
// Use for: Fixed number of parallel operations known at compile time async let user = fetchUser() async let posts = fetchPosts() let profile = try await (user, posts)
Task - Starting unstructured asynchronous work
// Use for: Fire-and-forget operations, bridging sync to async contexts Task { await updateUI() }
Task Group - Dynamic parallel operations with structured concurrency
// Use for: Unknown number of parallel operations at compile time await withTaskGroup(of: Result.self) { group in for item in items { group.addTask { await process(item) } } }
Actor - Protecting mutable state from data races
// Use for: Shared mutable state accessed from multiple contexts actor DataCache { private var cache: [String: Data] = [:] func get(_ key: String) -> Data? { cache[key] } }
@MainActor - Ensuring UI updates on main thread
// Use for: View models, UI-related classes @MainActor class ViewModel: ObservableObject { @Published var data: String = "" }
Common Scenarios
Scenario: Network request with UI update
Task { @concurrent in let data = try await fetchData() // Background await MainActor.run { self.updateUI(with: data) // Main thread } }
Scenario: Multiple parallel network requests
async let users = fetchUsers() async let posts = fetchPosts() async let comments = fetchComments() let (u, p, c) = try await (users, posts, comments)
Scenario: Processing array items in parallel
await withTaskGroup(of: ProcessedItem.self) { group in for item in items { group.addTask { await process(item) } } for await result in group { results.append(result) } }
Swift 6 Migration Quick Guide
Key changes in Swift 6:
- Strict concurrency checking enabled by default
- Complete data-race safety at compile time
- Sendable requirements enforced on boundaries
- Isolation checking for all async boundaries
For detailed migration steps, see
references/migration.md.
Reference Files
Load these files as needed for specific topics:
- async/await syntax, execution order, async let, URLSession patternsasync-await-basics.md
- Task lifecycle, cancellation, priorities, task groups, structured vs unstructuredtasks.md
- Thread/task relationship, suspension points, isolation domains, nonisolatedthreading.md
- Retain cycles in tasks, memory safety patternsmemory-management.md
- Actor isolation, @MainActor, global actors, reentrancy, custom executors, Mutexactors.md
- Sendable conformance, value/reference types, @unchecked, region isolationsendable.md
- AsyncSequence, AsyncStream, when to use vs regular async methodsasync-sequences.md
- NSManagedObject sendability, custom executors, isolation conflictscore-data.md
- Profiling with Instruments, reducing suspension points, execution strategiesperformance.md
- XCTest async patterns, Swift Testing, concurrency testing utilitiestesting.md
- Swift 6 migration strategy, closure-to-async conversion, @preconcurrency, FRP migrationmigration.md
Best Practices Summary
- Prefer structured concurrency - Use task groups over unstructured tasks when possible
- Minimize suspension points - Keep actor-isolated sections small to reduce context switches
- Use @MainActor judiciously - Only for truly UI-related code
- Make types Sendable - Enable safe concurrent access by conforming to Sendable
- Handle cancellation - Check Task.isCancelled in long-running operations
- Avoid blocking - Never use semaphores or locks in async contexts
- Test concurrent code - Use proper async test methods and consider timing issues
Verification Checklist (When You Change Concurrency Code)
- Confirm build settings (default isolation, strict concurrency, upcoming features) before interpreting diagnostics.
- After refactors:
- Run tests, especially concurrency-sensitive ones (see
).references/testing.md - If performance-related, verify with Instruments (see
).references/performance.md - If lifetime-related, verify deinit/cancellation behavior (see
).references/memory-management.md
- Run tests, especially concurrency-sensitive ones (see
Glossary
See
references/glossary.md for quick definitions of core concurrency terms used across this skill.
Note: This skill is based on the comprehensive Swift Concurrency Course by Antoine van der Lee.