skillfed

swift-concurrency-6-2

This skill guides you through Swift 6.2's concurrency paradigm, where code runs single-threaded by default and background work requires explicit @concurrent annotation. Learn how async functions now stay on the caller's actor, eliminating implicit thread offloading that caused data races in earlier versions. Master isolated protocol conformances for MainActor types and adopt MainActor inference to reduce boilerplate in app targets.

Swift Concurrency 6.2 helps you migrate projects safely by making async stay on the caller's actor and requiring explicit @concurrent for background work.

AI-generated summary based on this skill's SKILL.md

1,767 298 MIT updated by xu-xiang

Install

xu-xiang/everything-claude-code-zh/swift-concurrency-6-2 · repository language: JavaScript

CLI (skillfed)coming soon
git clone https://github.com/xu-xiang/everything-claude-code-zh
cp -r everything-claude-code-zh/skills/swift-concurrency-6-2 ~/.claude/skills/swift-concurrency-6-2

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

Is Swift 6.2 concurrency approachable for developers new to async?

swift-concurrency-6-2 makes concurrency approachable by defaulting to single-threaded execution—code runs on the caller's actor unless you explicitly mark work with @concurrent. This eliminates the implicit thread-hopping that caused data races in earlier Swift versions. You learn predictable patterns: async functions stay on their actor, MainActor inference reduces boilerplate in app targets, and isolated protocol conformances clarify which thread each method runs on.

How do I migrate Swift 5 to 6.2 concurrency?

swift-concurrency-6-2 guides migration by teaching the core shift: async functions no longer implicitly offload to background threads. Instead, they inherit the caller's actor context. To move CPU-intensive work off the main thread, use explicit @concurrent annotation. Resolve compiler data-race errors by adopting Sendable constraints, protecting global static variables, and applying MainActor isolation to UI types. The skill covers isolated protocol conformances so your types conform safely on the main thread.

What is MainActor isolation and how does protocol conformance work?

swift-concurrency-6-2 explains that MainActor isolation ensures code runs on the main thread. With isolated conformances, you declare that a type's protocol methods execute on MainActor without annotating each method individually. MainActor inference in app targets automatically applies this isolation to UI types, reducing boilerplate. This pattern prevents data races by guaranteeing thread safety for UI updates and main-thread-only operations.

How do I use @concurrent for background work in Swift 6.2?

swift-concurrency-6-2 teaches explicit @concurrent offloading: mark CPU-intensive functions with @concurrent to run them off the caller's actor on a background thread. This replaces the implicit thread-hopping of earlier versions. The skill covers when and how to apply @concurrent, ensuring your async functions stay on their actor by default while giving you precise control over which work runs in the background, eliminating data races.

How does Swift 6.2 eliminate data races with single-threaded-by-default?

swift-concurrency-6-2 shows how single-threaded-by-default architecture prevents data races: code runs on one actor unless explicitly offloaded. Async functions inherit the caller's actor instead of jumping threads unpredictably. Sendable constraints and MainActor isolation enforce thread-safe data sharing. The compiler catches data-race errors at build time. This model replaces the implicit concurrency of earlier Swift versions, making thread safety the default rather than an afterthought.

What compiler errors will I encounter when adopting Swift 6.2?

swift-concurrency-6-2 helps resolve common data-race errors: non-Sendable types crossing actor boundaries, unprotected global static variables, and missing MainActor annotations on UI code. The skill teaches how to apply Sendable conformances, use isolated properties, and adopt MainActor inference in app targets. Understanding these errors—and the patterns to fix them—is central to migrating existing projects and writing new code that compiles without data-race warnings.

SKILL.md

rendered from the published skill — quoted content, verbatim

Swift 6.2 易用的并发 (Approachable Concurrency)

采用 Swift 6.2 并发模型的模式:代码默认在单线程运行,并发需显式引入。在不牺牲性能的情况下消除常见的数据竞争(Data-race)错误。

何时激活

  • 将 Swift 5.x 或 6.0/6.1 项目迁移到 Swift 6.2
  • 解决数据竞争安全的编译器错误
  • 设计基于 MainActor 的应用架构
  • 将 CPU 密集型工作卸载到后台线程
  • MainActor 隔离的类型上实现协议一致性(Protocol conformances)
  • 在 Xcode 26 中启用易用的并发(Approachable Concurrency)构建设置

核心问题:隐式后台卸载

在 Swift 6.1 及更早版本中,异步函数(Async functions)可能会被隐式卸载到后台线程,即使在看似安全的代码中也会导致数据竞争错误:

// 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 }

        // 错误:发送 'self.photoProcessor' 存在引起数据竞争的风险
        return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
    }
}

Swift 6.2 修复了这个问题:异步函数默认保留在调用方执行角色(Actor)上。

```swift // Swift 6.2: 正常 (OK) — 异步保留在 MainActor,无数据竞争 @MainActor final class StickerModel { let photoProcessor =

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
skills/swift-concurrency-6-2/SKILL.md

Related skills

Tags

actor-isolation thread-safety compile-time-safety async-await ui-threading background-offload data-race-prevention incremental-adoption performance-tuning swift-evolution