Swift concurrency

From ComeOnOliver/skillshub · View interactive version

Swift concurrency is an AI agent skill from the GitHub repository ComeOnOliver/skillshub. Diagnose data races, convert callback-based code to async/await, implement actor isolation patterns, resolve Sendable conformance issues, and guide Swift 6 migration. Use when developers mention: (1) Swift Concurrency, async/await, actors, or tasks, (2) "use Swift Concurrency" or "modern concurrency

How to use this skill

Skill file

Swift Concurrency

Fast Path

Before proposing a fix:

  1. Analyze Package.swift or .pbxproj to determine Swift language mode, strict concurrency level, default isolation, and upcoming features. Do this always, not only for migration work.
  2. Capture the exact diagnostic and offending symbol.
  3. Determine the isolation boundary: @MainActor, custom actor, actor instance isolation, or nonisolated.
  4. Confirm whether the code is UI-bound or intended to run off the main actor.

Project settings that change concurrency behavior:

| Setting | SwiftPM (Package.swift) | Xcode (.pbxproj) |

|---|---|---|

| Language mode | swiftLanguageVersions or -swift-version (// swift-tools-version: is not a reliable proxy) | Swift Language Version |

| Strict concurrency | .enableExperimentalFeature("StrictConcurrency=targeted") | SWIFT_STRICT_CONCURRENCY |

| Default isolation | .defaultIsolation(MainActor.self) | SWIFT_DEFAULT_ACTOR_ISOLATION |

| Upcoming features | .enableUpcomingFeature("NonisolatedNonsendingByDefault") | SWIFT_UPCOMING_FEATURE_* |

If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance. Do not guess.

Guardrails:

Quick Fix Mode

Use Quick Fix Mode when all of these are true:

Skip Quick Fix Mode when any of these are true:

Common Diagnostics

| Diagnostic | First check | Smallest safe fix | Escalate to |

|---|---|---|---|

| Main actor-isolated ... cannot be used from a nonisolated context | Is this truly UI-bound? | Isolate the caller to @MainActor or use await MainActor.run { ... } only when main-actor ownership is correct. | references/actors.md, references/threading.md |

| Actor-isolated type does not conform to protocol | Must the requirement run on the actor? | Prefer isolated conformance (e.g., extension Foo: @MainActor SomeProtocol); use nonisolated only for truly nonisolated requirements. | references/actors.md |

| Sending value of non-Sendable type ... risks causing data races | What isolation boundary is being crossed? | Keep access inside one actor, or convert the transferred value to an immutable/value type. | references/sendable.md, references/threading.md |

| SwiftLint async_without_await | Is async actually required by protocol, override, or @concurrent? | Remove async, or use a narrow suppression with rationale. Never add fake awaits. | references/linting.md |

| wait(...) is unavailable from asynchronous contexts | Is this legacy XCTest async waiting? | Replace with await fulfillment(of:) or Swift Testing equivalents. | references/testing.md |

| Core Data concurrency warnings | Are NSManagedObject instances crossing contexts or actors? | Pass NSManagedObjectID or map to a Sendable value type. | references/core-data.md |

| Thread.current unavailable from asynchronous contexts | Are you debugging by thread instead of isolation? | Reason in terms of isolation and use Instruments/debugger instead. | references/threading.md |

| SwiftLint concurrency-related warnings | Which specific lint rule triggered? | Use references/linting.md for rule intent and preferred fixes; avoid dummy awaits. | references/linting.md |

When Quick Fixes Fail

  1. Gather project settings if not already confirmed.
  2. Re-evaluate which isolation boundaries the type crosses.
  3. Route to the matching reference file for a deeper fix.
  4. If the fix may change behavior, document the invariant and add verification steps.

Smallest Safe Fixes

Prefer changes that preserve behavior while satisfying data-race safety:

Concurrency Tool Selection

| Need | Tool | Key Guidance |

|---|---|---|

| Single async operation | async/await | Default choice for sequential async work |

| Fixed parallel operations | async let | Known count at compile time; auto-cancelled on throw |

| Dynamic parallel operations | withTaskGroup | Unknown count; structured — cancels children on scope exit |

| Sync → async bridge | Task { } | Inherits actor context; use Task.detached only with documented reason |

| Shared mutable state | actor | Prefer over locks/queues; keep isolated sections small |

| UI-bound state | @MainActor | Only for truly UI-related code; justify isolation |

Common Scenarios

Network request with UI update

Task { @concurrent in
    let data = try await fetchData()
    await MainActor.run { self.updateUI(with: data) }
}

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:

Migration Validation Loop

Apply this cycle for each migration change:

  1. Build — Run swift build or Xcode build to surface new diagnostics
  2. Fix — Address one category of error at a time (e.g., all Sendable issues first)
  3. Rebuild — Confirm the fix compiles cleanly before moving on
  4. Test — Run the test suite to catch regressions (swift test or Cmd+U)
  5. Only proceed to the next file/module when all diagnostics are resolved

If a fix introduces new warnings, resolve them before continuing. Never batch multiple unrelated fixes — keep commits small and reviewable.

For detailed migration steps, see references/migration.md.

Reference Router

Open the smallest reference that matches the question:

Verification Checklist

When changing concurrency code:

  1. Re-check build settings before interpreting diagnostics.
  2. Build and clear one category of errors before moving on. Do not batch unrelated fixes into the same change.
  3. Run tests, especially actor-, lifetime-, and cancellation-sensitive tests.
  4. Use Instruments for performance claims instead of guessing.
  5. Verify deallocation and cancellation behavior for long-lived tasks.
  6. Check Task.isCancelled in long-running operations.
  7. Never use semaphores or ad hoc locking in async contexts when actor isolation or Mutex would express ownership more safely.

Note: This skill is based on the comprehensive Swift Concurrency Course by Antoine van der Lee.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.