swift-concurrency
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwift Concurrency
Swift 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.
本技能提供关于Swift Concurrency的专业指导,涵盖现代async/await模式、actors、tasks、Sendable合规性以及向Swift 6的迁移。借助本技能,开发者可以编写安全、高性能的并发代码,并驾驭Swift结构化并发模型的复杂性。
Agent Behavior Contract (Follow These Rules)
Agent行为约定(请遵循以下规则)
- 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.
- 当建议依赖Swift语言版本时,分析项目/包文件以确定使用的Swift语言模式(Swift 5.x vs Swift 6)以及Xcode/Swift工具链。
- 在提出修复方案前,明确隔离边界:、自定义actor、actor实例隔离或非隔离。
@MainActor - 不要将作为万能修复方案。需说明为何主actor隔离对该代码是合理的。
@MainActor - 优先使用结构化并发(子任务、任务组)而非非结构化任务。仅在有明确理由时使用。
Task.detached - 若推荐使用、
@preconcurrency或@unchecked Sendable,需满足:nonisolated(unsafe)- 有文档记录的安全不变量
- 创建后续工单以移除或迁移该用法
- 对于迁移工作,优化以最小化影响范围(小而易于评审的变更)并添加验证步骤。
- 课程参考仅用于深度学习。仅在其能明确帮助解答开发者问题时才少量使用。
Recommended Tools for Analysis
推荐的分析工具
When analyzing Swift projects for concurrency issues:
- Project Settings Discovery
- Use on
Readfor SwiftPM settings (tools version, strict concurrency flags, upcoming features)Package.swift - Use for
GreporSWIFT_STRICT_CONCURRENCYinSWIFT_DEFAULT_ACTOR_ISOLATIONfiles.pbxproj - Use for
Grepto find enabled upcoming featuresSWIFT_UPCOMING_FEATURE_
- Use
分析Swift项目的并发问题时:
- 项目设置探测
- 对使用
Package.swift操作获取SwiftPM设置(工具版本、严格并发标志、即将推出的特性)Read - 对文件使用
.pbxproj操作查找Grep或SWIFT_STRICT_CONCURRENCYSWIFT_DEFAULT_ACTOR_ISOLATION - 使用操作查找
Grep以了解已启用的即将推出的特性SWIFT_UPCOMING_FEATURE_
- 对
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
并发行为依赖于构建设置。请务必确定:
- 默认actor隔离(模块默认是还是
@MainActor?)nonisolated - 严格并发检查级别(minimal/targeted/complete)
- 是否启用了即将推出的特性(尤其是)
NonisolatedNonsendingByDefault - Swift语言模式(Swift 5.x vs Swift 6)以及SwiftPM工具版本
Manual checks (no scripts)
手动检查(无需脚本)
- SwiftPM:
- Check for
Package.swift..defaultIsolation(MainActor.self) - Check for
Package.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/or
SWIFT_UPCOMING_FEATURE_)SWIFT_ENABLE_EXPERIMENTAL_FEATURES
- Search
If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance.
- SwiftPM:
- 检查中是否包含
Package.swift。.defaultIsolation(MainActor.self) - 检查中是否包含
Package.swift。.enableUpcomingFeature("NonisolatedNonsendingByDefault") - 检查是否存在严格并发标志:(或类似配置)。
.enableExperimentalFeature("StrictConcurrency=targeted") - 查看顶部的工具版本:
// swift-tools-version: ...
- 检查
- Xcode项目:
- 在中搜索:
project.pbxprojSWIFT_DEFAULT_ACTOR_ISOLATIONSWIFT_STRICT_CONCURRENCY- (和/或
SWIFT_UPCOMING_FEATURE_)SWIFT_ENABLE_EXPERIMENTAL_FEATURES
- 在
如果以上任何信息未知,请先让开发者确认,再提供对迁移敏感的指导。
Quick Decision Tree
快速决策树
When a developer needs concurrency guidance, follow this decision tree:
-
Starting fresh with async code?
- Read for foundational patterns
references/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 isolation
references/threading.md
- Read
-
Memory issues with tasks?
- Read for retain cycle prevention
references/memory-management.md
- Read
当开发者需要并发指导时,请遵循以下决策树:
-
从零开始编写异步代码?
- 阅读了解基础模式
references/async-await-basics.md - 并行操作 → 参考(async let、任务组)
references/tasks.md
- 阅读
-
保护共享可变状态?
- 需要保护基于类的状态 → 参考(actors、@MainActor)
references/actors.md - 需要线程安全的值传递 → 参考(Sendable合规性)
references/sendable.md
- 需要保护基于类的状态 → 参考
-
管理异步操作?
- 结构化异步工作 → 参考(Task、子任务、取消)
references/tasks.md - 流式数据 → 参考(AsyncSequence、AsyncStream)
references/async-sequences.md
- 结构化异步工作 → 参考
-
与遗留框架交互?
- Core Data集成 → 参考
references/core-data.md - 通用迁移 → 参考
references/migration.md
- Core Data集成 → 参考
-
性能或调试问题?
- 异步代码运行缓慢 → 参考(性能分析、挂起点)
references/performance.md - 测试相关问题 → 参考(XCTest、Swift Testing)
references/testing.md
- 异步代码运行缓慢 → 参考
-
理解线程行为?
- 阅读了解线程/任务关系与隔离
references/threading.md
- 阅读
-
任务相关的内存问题?
- 阅读了解如何避免保留环
references/memory-management.md
- 阅读
Triage-First Playbook (Common Errors -> Next Best Move)
优先排查手册(常见错误 → 下一步最佳行动)
- SwiftLint concurrency-related warnings
- Use for rule intent and preferred fixes; avoid dummy awaits as “fixes”.
references/linting.md
- Use
- SwiftLint warning
async_without_await- Remove if not required; if required by protocol/override/@concurrent, prefer narrow suppression over adding fake awaits. See
async.references/linting.md
- Remove
- "Sending value of non-Sendable type ... risks causing data races"
- First: identify where the value crosses an isolation boundary
- Then: use and
references/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 + Instruments
references/threading.md
- Use
- XCTest async errors like "wait(...) is unavailable from asynchronous contexts"
- Use (
references/testing.mdand Swift Testing patterns)await fulfillment(of:)
- Use
- Core Data concurrency warnings/errors
- Use (DAO/
references/core-data.md, default isolation conflicts)NSManagedObjectID
- Use
- SwiftLint并发相关警告
- 参考了解规则意图与推荐修复方案;避免将虚假await作为“修复手段”。
references/linting.md
- 参考
- SwiftLint 警告
async_without_await- 如果不需要则移除;如果是协议/重写/@concurrent要求的,优先使用窄范围抑制而非添加虚假await。请参考
async。references/linting.md
- 如果不需要则移除
- “Sending value of non-Sendable type ... risks causing data races”(发送非Sendable类型的值...可能导致数据竞争)
- 第一步:确定值在何处跨越隔离边界
- 第二步:参考和
references/sendable.md(尤其是Swift 6.2的行为变更)references/threading.md
- “Main actor-isolated ... cannot be used from a nonisolated context”(主actor隔离的...无法在非隔离上下文中使用)
- 第一步:判断该代码是否确实属于
@MainActor - 第二步:参考(全局actors、
references/actors.md、隔离参数)和nonisolated(默认隔离)references/threading.md
- 第一步:判断该代码是否确实属于
- “Class property 'current' is unavailable from asynchronous contexts”(Thread API相关)
- 参考避免以线程为中心的调试,转而依赖隔离 + Instruments
references/threading.md
- 参考
- XCTest异步错误,如“wait(...) is unavailable from asynchronous contexts”
- 参考(
references/testing.md和Swift Testing模式)await fulfillment(of:)
- 参考
- Core Data并发警告/错误
- 参考(DAO/
references/core-data.md、默认隔离冲突)NSManagedObjectID
- 参考
Core Patterns Reference
核心模式参考
When to Use Each Concurrency Tool
各并发工具的适用场景
async/await - Making existing synchronous code asynchronous
swift
// Use for: Single asynchronous operations
func fetchUser() async throws -> User {
try await networkClient.get("/user")
}async let - Running multiple independent async operations in parallel
swift
// 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
swift
// Use for: Fire-and-forget operations, bridging sync to async contexts
Task {
await updateUI()
}Task Group - Dynamic parallel operations with structured concurrency
swift
// 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
swift
// 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
swift
// Use for: View models, UI-related classes
@MainActor
class ViewModel: ObservableObject {
@Published var data: String = ""
}async/await - 将现有同步代码改造为异步
swift
// Use for: Single asynchronous operations
func fetchUser() async throws -> User {
try await networkClient.get("/user")
}async let - 并行运行多个独立的异步操作
swift
// 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 - 启动非结构化异步工作
swift
// Use for: Fire-and-forget operations, bridging sync to async contexts
Task {
await updateUI()
}Task Group - 采用结构化并发的动态并行操作
swift
// 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 - 保护可变状态避免数据竞争
swift
// Use for: Shared mutable state accessed from multiple contexts
actor DataCache {
private var cache: [String: Data] = [:]
func get(_ key: String) -> Data? { cache[key] }
}@MainActor - 确保UI更新在主线程执行
swift
// Use for: View models, UI-related classes
@MainActor
class ViewModel: ObservableObject {
@Published var data: String = ""
}Common Scenarios
常见场景
Scenario: Network request with UI update
swift
Task { @concurrent in
let data = try await fetchData() // Background
await MainActor.run {
self.updateUI(with: data) // Main thread
}
}Scenario: Multiple parallel network requests
swift
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
swift
await withTaskGroup(of: ProcessedItem.self) { group in
for item in items {
group.addTask { await process(item) }
}
for await result in group {
results.append(result)
}
}场景:网络请求与UI更新
swift
Task { @concurrent in
let data = try await fetchData() // Background
await MainActor.run {
self.updateUI(with: data) // Main thread
}
}场景:多个并行网络请求
swift
async let users = fetchUsers()
async let posts = fetchPosts()
async let comments = fetchComments()
let (u, p, c) = try await (users, posts, comments)场景:并行处理数组元素
swift
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
Swift 6迁移快速指南
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.mdSwift 6中的关键变更:
- 严格并发检查默认启用
- 编译时完全数据竞争安全
- 边界处强制要求Sendable
- 所有异步边界的隔离检查
详细迁移步骤请参考。
references/migration.mdReference Files
参考文件
Load these files as needed for specific topics:
- - async/await syntax, execution order, async let, URLSession patterns
async-await-basics.md - - Task lifecycle, cancellation, priorities, task groups, structured vs unstructured
tasks.md - - Thread/task relationship, suspension points, isolation domains, nonisolated
threading.md - - Retain cycles in tasks, memory safety patterns
memory-management.md - - Actor isolation, @MainActor, global actors, reentrancy, custom executors, Mutex
actors.md - - Sendable conformance, value/reference types, @unchecked, region isolation
sendable.md - - Concurrency-focused lint rules and SwiftLint
linting.mdasync_without_await - - AsyncSequence, AsyncStream, when to use vs regular async methods
async-sequences.md - - NSManagedObject sendability, custom executors, isolation conflicts
core-data.md - - Profiling with Instruments, reducing suspension points, execution strategies
performance.md - - XCTest async patterns, Swift Testing, concurrency testing utilities
testing.md - - Swift 6 migration strategy, closure-to-async conversion, @preconcurrency, FRP migration
migration.md
根据具体主题按需加载以下文件:
- - async/await语法、执行顺序、async let、URLSession模式
async-await-basics.md - - Task生命周期、取消、优先级、任务组、结构化vs非结构化
tasks.md - - 线程/任务关系、挂起点、隔离域、非隔离
threading.md - - 任务中的保留环、内存安全模式
memory-management.md - - Actor隔离、@MainActor、全局actors、可重入性、自定义执行器、Mutex
actors.md - - Sendable合规性、值/引用类型、@unchecked、区域隔离
sendable.md - - 并发相关lint规则与SwiftLint
linting.mdasync_without_await - - AsyncSequence、AsyncStream、与常规异步方法的适用场景对比
async-sequences.md - - NSManagedObject可发送性、自定义执行器、隔离冲突
core-data.md - - 使用Instruments进行性能分析、减少挂起点、执行策略
performance.md - - XCTest异步模式、Swift Testing、并发测试工具
testing.md - - Swift 6迁移策略、闭包转异步、@preconcurrency、FRP迁移
migration.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
- 优先使用结构化并发 - 尽可能使用任务组而非非结构化任务
- 最小化挂起点 - 缩小actor隔离代码段的范围以减少上下文切换
- 谨慎使用@MainActor - 仅用于真正与UI相关的代码
- 让类型符合Sendable - 通过遵循Sendable实现安全的并发访问
- 处理取消 - 在长时间运行的操作中检查Task.isCancelled
- 避免阻塞 - 切勿在异步上下文中使用信号量或锁
- 测试并发代码 - 使用正确的异步测试方法并考虑时序问题
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
- 解释诊断信息前,请确认构建设置(默认隔离、严格并发、即将推出的特性)。
- 重构后:
- 运行测试,尤其是对并发敏感的测试(参考)。
references/testing.md - 如果涉及性能,请使用Instruments验证(参考)。
references/performance.md - 如果涉及生命周期,请验证deinit/取消行为(参考)。
references/memory-management.md
- 运行测试,尤其是对并发敏感的测试(参考
Glossary
术语表
See for quick definitions of core concurrency terms used across this skill.
references/glossary.mdNote: This skill is based on the comprehensive Swift Concurrency Course by Antoine van der Lee.