swift-concurrency

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Swift Concurrency Patterns

Swift 并发模式

Expert guidance on Swift Concurrency best practices covering async/await, actors, tasks, Sendable, threading, memory management, testing, and migration strategies.
关于Swift并发最佳实践的专业指南,涵盖async/await、actors、任务、Sendable、线程、内存管理、测试以及迁移策略。

Rules

规则

Core Swift Concurrency rules extracted as discrete, high-impact patterns. See rules index for the full list organized by:
  • Async/Await Patterns - Never add dummy suspension points to silence warnings
  • Actor Isolation - Use actors for data-race safety with compiler verification
  • Task Lifecycle - Understand task cancellation and structured concurrency
  • Sendable Conformance - Require Sendable for crossing concurrency boundaries
  • Testing - Patterns for testing async concurrent code without flaky tests
核心Swift并发规则被提炼为独立、高影响力的模式。请参阅规则索引获取完整列表,按以下类别组织:
  • Async/Await 模式 - 切勿添加虚拟挂起点来消除警告
  • Actor 隔离 - 使用actors实现编译器验证的数据竞争安全
  • 任务生命周期 - 理解任务取消与结构化并发
  • Sendable 一致性 - 跨并发边界时要求遵循Sendable
  • 测试 - 用于测试异步并发代码且避免不稳定测试的模式

References

参考资料

See references/swift-concurrency.md for comprehensive guidance organized by:
  • Async/Await Fundamentals - Core patterns, error handling, parallel execution
  • Tasks & Structured Concurrency - Task lifecycle, cancellation, task groups
  • Actors & Isolation - Actor isolation, suspension points, state safety
  • Sendable & Data Safety - Sendable conformance, data races, safe captures
  • Threading & Execution - Execution contexts, isolation domains
  • Memory Management - Retain cycles, weak references, task lifecycle
  • Testing Concurrency - Async test patterns, Swift Testing integration
  • Migration & Interop - Strict concurrency migration, legacy interop
请参阅references/swift-concurrency.md获取全面指南,按以下类别组织:
  • Async/Await 基础 - 核心模式、错误处理、并行执行
  • 任务与结构化并发 - 任务生命周期、取消、任务组
  • Actors 与隔离 - Actor隔离、挂起点、状态安全
  • Sendable 与数据安全 - Sendable一致性、数据竞争、安全捕获
  • 线程与执行 - 执行上下文、隔离域
  • 内存管理 - 保留循环、弱引用、任务生命周期
  • 并发测试 - 异步测试模式、Swift Testing集成
  • 迁移与互操作 - 严格并发迁移、遗留代码互操作

Examples

示例

Positive Trigger

触发场景

User: "Refactor callback-based network code to async/await with actor isolation."
Expected behavior: Use
swift-concurrency
guidance, follow its workflow, and return actionable output.
用户:"将基于回调的网络代码重构为带有actor隔离的async/await代码。"
预期行为:遵循
swift-concurrency
指南,按照其工作流程,返回可执行的输出。

Non-Trigger

非触发场景

User: "Refactor CSS grid layout for mobile breakpoints."
Expected behavior: Do not prioritize
swift-concurrency
; choose a more relevant skill or proceed without it.
用户:"为移动端断点重构CSS网格布局。"
预期行为:不优先使用
swift-concurrency
;选择更相关的技能或直接处理。

Troubleshooting

故障排除

Skill Does Not Trigger

技能未触发

  • Error: The skill is not selected when expected.
  • Cause: Request wording does not clearly match the description trigger conditions.
  • Solution: Rephrase with explicit domain/task keywords from the description and retry.
  • 错误:预期时技能未被选中。
  • 原因:请求措辞未明确匹配描述中的触发条件。
  • 解决方案:使用描述中的明确领域/任务关键词重新措辞,并重试。

Guidance Conflicts With Another Skill

指南与其他技能冲突

  • Error: Instructions from multiple skills conflict in one task.
  • Cause: Overlapping scope across loaded skills.
  • Solution: State which skill is authoritative for the current step and apply that workflow first.
  • 错误:同一任务中多个技能的说明冲突。
  • 原因:已加载技能的范围重叠。
  • 解决方案:指定当前步骤的权威技能,并优先应用其工作流程。

Output Is Too Generic

输出过于笼统

  • Error: Result lacks concrete, actionable detail.
  • Cause: Task input omitted context, constraints, or target format.
  • Solution: Add specific constraints (environment, scope, format, success criteria) and rerun.
  • 错误:结果缺乏具体、可执行的细节。
  • 原因:任务输入遗漏了上下文、约束或目标格式。
  • 解决方案:添加具体约束(环境、范围、格式、成功标准),并重试。

Workflow

工作流程

  1. Identify whether the request clearly matches
    swift-concurrency
    scope and triggers.
  2. Apply the skill rules and referenced guidance to produce a concrete result.
  3. Validate output quality against constraints; if gaps remain, refine once with explicit assumptions.
  1. 判断请求是否明确匹配
    swift-concurrency
    的范围与触发条件。
  2. 应用技能规则与参考指南生成具体结果。
  3. 根据约束验证输出质量;若仍有不足,基于明确假设优化一次。