swift-concurrency by avdlee/swift-concurrency-agent-skill
npx skills add https://github.com/avdlee/swift-concurrency-agent-skill --skill swift-concurrencyPackage.swift 或 .pbxproj 以确定 Swift 语言模式(5.x 与 6)和工具链。@MainActor、自定义 actor、actor 实例隔离或非隔离。@MainActor 作为通用的修复方案。需论证为何主 actor 隔离适用于该代码。Task.detached。@preconcurrency、@unchecked Sendable 或 nonisolated(unsafe),必须满足:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@MainActor、自定义 actor、默认隔离)。在以下情况使用快速修复模式:
在以下情况跳过快速修复模式:
@unchecked Sendable、@preconcurrency 或 nonisolated(unsafe) 但没有明确的不变量。并发行为取决于构建设置。在给出建议前,通过 Read 操作读取 Package.swift 或通过 Grep 在 .pbxproj 文件中确定以下设置:
| 设置项 | SwiftPM (Package.swift) | Xcode (.pbxproj) |
|---|---|---|
| 默认隔离 | .defaultIsolation(MainActor.self) | SWIFT_DEFAULT_ACTOR_ISOLATION |
| 严格并发 | .enableExperimentalFeature("StrictConcurrency=targeted") | SWIFT_STRICT_CONCURRENCY |
| 即将推出的功能 | .enableUpcomingFeature("NonisolatedNonsendingByDefault") | SWIFT_UPCOMING_FEATURE_* |
| 语言模式 | 顶部的 // swift-tools-version: | Swift Language Version 构建设置 |
如果其中任何一项未知,请在给出与迁移相关的指导前,要求开发者确认它们。
优先选择在满足数据竞争安全性的同时保留行为的编辑操作。
@MainActor(需论证为何是 UI 绑定)。actor,或者如果是仅 UI 相关的,则隔离到 @MainActor。@concurrent 的 async 函数;对于不接触隔离状态但可以继承调用方隔离的工作(例如使用 NonisolatedNonsendingByDefault),使用不带 @concurrent 的 nonisolated,或使用 actor 来保护可变状态。@unchecked Sendable,除非你能证明并记录线程安全性。@MainActor 或使用 await MainActor.run { ... } 跳转。references/actors.md。extension Foo: @MainActor SomeProtocol)。nonisolated,则升级处理;使用 references/actors.md。let)状态的值类型。@unchecked Sendable 之前升级处理;使用 references/sendable.md 和 references/threading.md。async_without_await
async;如果协议/重写/@concurrent 要求,则使用附带理由的局部抑制。参见 references/linting.md。await fulfillment(of:) 或 Swift Testing 的等效方法。参见 references/testing.md。当开发者需要并发指导时,遵循此决策树:
references/async-await-basics.md 了解基础模式references/tasks.md (async let, 任务组)references/actors.md (actors, @MainActor)references/sendable.md (Sendable 一致性)references/tasks.md (Task, 子任务, 取消)references/async-sequences.md (AsyncSequence, AsyncStream)references/core-data.mdreferences/migration.mdreferences/performance.md (性能分析, 挂起点)references/testing.md (XCTest, Swift Testing)references/threading.md 了解线程/任务关系和隔离references/memory-management.md 了解如何防止循环引用references/linting.md 了解规则意图和首选修复方案;避免使用虚拟 await 作为“修复”。async_without_await 警告
async;如果协议/重写/@concurrent 要求,优先选择局部抑制而非添加虚假的 await。参见 references/linting.md。references/sendable.md 和 references/threading.md(特别是 Swift 6.2 的行为变化)@MainActorreferences/actors.md(全局 actor、nonisolated、隔离参数)和 references/threading.md(默认隔离)references/threading.md 避免以线程为中心的调试,并依赖隔离 + Instruments@MainActor 上)或者可以安全地设为 nonisolated。references/actors.md 中的实现模式(隔离一致性、nonisolated 要求和升级步骤)。references/testing.md (await fulfillment(of:) 和 Swift Testing 模式)references/core-data.md (DAO/NSManagedObjectID、默认隔离冲突)| 需求 | 工具 | 关键指导 |
|---|---|---|
| 单一异步操作 | async/await | 顺序异步工作的默认选择 |
| 固定数量的并行操作 | async let | 编译时已知数量;抛出时自动取消 |
| 动态数量的并行操作 | withTaskGroup | 数量未知;结构化 — 在作用域退出时取消子任务 |
| 同步 → 异步桥接 | Task { } | 继承 actor 上下文;仅在理由明确时使用 Task.detached |
| 共享可变状态 | actor | 优先于锁/队列;保持隔离部分短小 |
| UI 绑定状态 | @MainActor | 仅用于真正与 UI 相关的代码;论证隔离理由 |
网络请求与 UI 更新
Task { @concurrent in
let data = try await fetchData()
await MainActor.run { self.updateUI(with: data) }
}
并行处理数组项
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 的主要变化:
对每个迁移更改应用此循环:
swift build 或 Xcode 构建以显示新的诊断信息swift test 或 Cmd+U)如果修复引入了新的警告,请在继续之前解决它们。切勿批量处理多个不相关的修复 — 保持提交内容小而可审查。
有关详细的迁移步骤,请参见 references/migration.md。
根据需要加载这些文件以获取特定主题:
async-await-basics.md - async/await 语法、执行顺序、async let、URLSession 模式tasks.md - Task 生命周期、取消、优先级、任务组、结构化与非结构化threading.md - 线程/任务关系、挂起点、隔离域、非隔离memory-management.md - 任务中的循环引用、内存安全模式actors.md - Actor 隔离、@MainActor、全局 actor、重入、自定义执行器、Mutexsendable.md - Sendable 一致性、值/引用类型、@unchecked、区域隔离linting.md - 专注于并发的 lint 规则和 SwiftLint async_without_awaitasync-sequences.md - AsyncSequence、AsyncStream、何时使用 vs 常规异步方法core-data.md - NSManagedObject 可发送性、自定义执行器、隔离冲突performance.md - 使用 Instruments 进行性能分析、减少挂起点、执行策略testing.md - XCTest 异步模式、Swift Testing、并发测试工具migration.md - Swift 6 迁移策略、闭包到异步转换、@preconcurrency、FRP 迁移references/testing.md)。references/performance.md)。references/memory-management.md)。Task.isCancelled。Mutex。有关本技能中使用的核心并发术语的快速定义,请参见 references/glossary.md。
注意:本技能基于 Antoine van der Lee 的综合性 Swift Concurrency Course。
每周安装量
3.6K
代码仓库
GitHub Stars
1.2K
首次出现
Jan 21, 2026
安全审计
安装于
codex3.0K
opencode2.8K
github-copilot2.7K
gemini-cli2.7K
amp2.3K
kimi-cli2.3K
Package.swift or .pbxproj to determine Swift language mode (5.x vs 6) and toolchain before giving advice.@MainActor, custom actor, actor instance isolation, or nonisolated.@MainActor as a blanket fix. Justify why main-actor isolation is correct for the code.Task.detached only with a clear reason.@preconcurrency, @unchecked Sendable, or nonisolated(unsafe), require:
@MainActor, custom actor, default isolation).Use Quick Fix Mode when:
Skip Quick Fix Mode when:
@unchecked Sendable, @preconcurrency, or nonisolated(unsafe) without a clear invariant.Concurrency behavior depends on build settings. Before advising, determine these via Read on Package.swift or Grep in .pbxproj files:
| Setting | SwiftPM (Package.swift) | Xcode (.pbxproj) |
|---|---|---|
| Default isolation | .defaultIsolation(MainActor.self) | SWIFT_DEFAULT_ACTOR_ISOLATION |
| Strict concurrency | .enableExperimentalFeature("StrictConcurrency=targeted") | SWIFT_STRICT_CONCURRENCY |
| Upcoming features | .enableUpcomingFeature("NonisolatedNonsendingByDefault") |
If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance.
Prefer edits that preserve behavior while satisfying data-race safety.
@MainActor (justify why UI-bound).actor or isolate to @MainActor if UI-only.async function marked @concurrent; for work that doesn’t touch isolated state but can inherit the caller’s isolation (for example with NonisolatedNonsendingByDefault), use nonisolated without @concurrent, or use an actor to guard mutable state.@MainActor or hop with await MainActor.run { ... }.references/actors.md.extension Foo: @MainActor SomeProtocol).nonisolated; use references/actors.md.let) state.When a developer needs concurrency guidance, follow this decision tree:
Starting fresh with async code?
references/async-await-basics.md for foundational patternsreferences/tasks.md (async let, task groups)Protecting shared mutable state?
references/actors.md (actors, @MainActor)references/sendable.md (Sendable conformance)Managing async operations?
references/tasks.md (Task, child tasks, cancellation)references/async-sequences.md (AsyncSequence, AsyncStream)references/linting.md for rule intent and preferred fixes; avoid dummy awaits as “fixes”.async_without_await warning
async if not required; if required by protocol/override/@concurrent, prefer narrow suppression over adding fake awaits. See references/linting.md.references/sendable.md and references/threading.md (especially Swift 6.2 behavior changes)@MainActor| 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 |
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)
}
}
Key changes in Swift 6:
Apply this cycle for each migration change:
swift build or Xcode build to surface new diagnosticsswift test or Cmd+U)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.
Load these files as needed for specific topics:
async-await-basics.md - async/await syntax, execution order, async let, URLSession patternstasks.md - Task lifecycle, cancellation, priorities, task groups, structured vs unstructuredthreading.md - Thread/task relationship, suspension points, isolation domains, nonisolatedmemory-management.md - Retain cycles in tasks, memory safety patternsactors.md - Actor isolation, @MainActor, global actors, reentrancy, custom executors, Mutexsendable.md - Sendable conformance, value/reference types, @unchecked, region isolationlinting.md - Concurrency-focused lint rules and SwiftLint references/testing.md).references/performance.md).references/memory-management.md).Task.isCancelled in long-running operations.Mutex instead.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.
Weekly Installs
3.6K
Repository
GitHub Stars
1.2K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex3.0K
opencode2.8K
github-copilot2.7K
gemini-cli2.7K
amp2.3K
kimi-cli2.3K
97,600 周安装
SWIFT_UPCOMING_FEATURE_* |
| Language mode | // swift-tools-version: at top | Swift Language Version build setting |
@unchecked Sendable unless you can prove and document thread safety.@unchecked Sendable; use references/sendable.md and references/threading.md.async_without_await
async if not required; if required by protocol/override/@concurrent, use narrow suppression with rationale. See references/linting.md.await fulfillment(of:) or Swift Testing equivalents. See references/testing.md.Working with legacy frameworks?
references/core-data.mdreferences/migration.mdPerformance or debugging issues?
references/performance.md (profiling, suspension points)references/testing.md (XCTest, Swift Testing)Understanding threading behavior?
references/threading.md for thread/task relationship and isolationMemory issues with tasks?
references/memory-management.md for retain cycle preventionreferences/actors.md (global actors, nonisolated, isolated parameters) and references/threading.md (default isolation)references/threading.md to avoid thread-centric debugging and rely on isolation + Instruments@MainActor) or can safely be nonisolated.references/actors.md for implementation patterns (isolated conformances, nonisolated requirements, and escalation steps).references/testing.md (await fulfillment(of:) and Swift Testing patterns)references/core-data.md (DAO/NSManagedObjectID, default isolation conflicts)actor |
| Prefer over locks/queues; keep isolated sections small |
| UI-bound state | @MainActor | Only for truly UI-related code; justify isolation |
async_without_awaitasync-sequences.md - AsyncSequence, AsyncStream, when to use vs regular async methodscore-data.md - NSManagedObject sendability, custom executors, isolation conflictsperformance.md - Profiling with Instruments, reducing suspension points, execution strategiestesting.md - XCTest async patterns, Swift Testing, concurrency testing utilitiesmigration.md - Swift 6 migration strategy, closure-to-async conversion, @preconcurrency, FRP migration