guide-swift-concurrency

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Guide Skill — This is an expert workflow/pattern guide, not API reference documentation. Originally from twostraws/Swift-Concurrency-Agent-Skill by Paul Hudson. MIT License.
Guide Skill — 这是一份专家级工作流/模式指南,而非API参考文档。 原文出自 Paul Hudson 的 twostraws/Swift-Concurrency-Agent-Skill。采用 MIT 许可证。

Swift Concurrency Patterns

Swift并发模式

Review and write Swift concurrency code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems — do not nitpick or invent issues.
审查并编写Swift并发代码,确保正确性、现代API使用以及符合项目约定。仅报告真实存在的问题——不要吹毛求疵或凭空捏造问题。

Core Instructions

核心说明

  • Target Swift 6.2 or later with strict concurrency checking.
  • Prefer structured concurrency (task groups) over unstructured (
    Task {}
    ).
  • Prefer Swift concurrency over GCD for new code. GCD is still acceptable in low-level code, framework interop, or performance-critical synchronous work.
  • Do not suggest
    @unchecked Sendable
    to fix compiler errors — prefer actors, value types, or
    sending
    parameters.
  • 针对Swift 6.2或更高版本,启用严格并发检查。
  • 优先使用结构化并发(任务组)而非非结构化的
    Task {}
  • 新代码优先使用Swift并发而非GCD。在底层代码、框架互操作或性能关键的同步工作中,GCD仍然是可接受的。
  • 不要建议使用
    @unchecked Sendable
    来修复编译器错误——优先选择actors、值类型或
    sending
    参数。

Review Process

审查流程

  1. Scan for known-dangerous patterns using
    references/hotspots.md
    .
  2. Check for Swift 6.2 concurrency behavior using
    references/new-features.md
    .
  3. Validate actor usage for reentrancy and isolation using
    references/actors.md
    .
  4. Ensure structured concurrency is preferred using
    references/structured.md
    .
  5. Check unstructured task usage using
    references/unstructured.md
    .
  6. Verify cancellation handling using
    references/cancellation.md
    .
  7. Validate async stream and continuation usage using
    references/async-streams.md
    .
  8. Check bridging code between sync and async using
    references/bridging.md
    .
  9. Review legacy concurrency migrations using
    references/interop.md
    .
  10. Cross-check against common failure modes using
    references/bug-patterns.md
    .
  11. If strict-concurrency errors exist, map diagnostics to fixes using
    references/diagnostics.md
    .
  12. If reviewing tests, check async test patterns using
    references/testing.md
    .
If doing partial work, load only the relevant reference files.
  1. 使用
    references/hotspots.md
    扫描已知危险模式。
  2. 借助
    references/new-features.md
    检查Swift 6.2的并发行为。
  3. 通过
    references/actors.md
    验证actors的可重入性和隔离性使用情况。
  4. 确保优先采用结构化并发,参考
    references/structured.md
  5. 使用
    references/unstructured.md
    检查非结构化任务的使用。
  6. 通过
    references/cancellation.md
    验证取消操作的处理。
  7. 借助
    references/async-streams.md
    验证异步流和continuation的使用。
  8. 使用
    references/bridging.md
    检查同步与异步之间的桥接代码。
  9. 参考
    references/interop.md
    审查遗留并发代码的迁移。
  10. 通过
    references/bug-patterns.md
    对照常见故障模式。
  11. 若存在严格并发错误,使用
    references/diagnostics.md
    将诊断信息映射到修复方案。
  12. 若审查测试代码,使用
    references/testing.md
    检查异步测试模式。
若仅处理部分工作,只需加载相关的参考文件即可。

References

参考文档

TopicReference
Grep targets for code review — known-dangerous patterns
references/hotspots.md
Swift 6.2: default isolation,
@concurrent
,
Task.immediate
, isolated deinit
references/new-features.md
Actor reentrancy, global actor inference, isolation patterns
references/actors.md
Task groups,
async let
,
withDiscardingTaskGroup
, concurrency limits
references/structured.md
Task
vs
Task.detached
, when
Task {}
is a code smell
references/unstructured.md
Cooperative cancellation,
withTaskCancellationHandler
, broken patterns
references/cancellation.md
AsyncStream.makeStream(of:)
, continuation lifecycle, back-pressure
references/async-streams.md
Checked continuations, wrapping delegates,
@unchecked Sendable
references/bridging.md
GCD migration, Combine to AsyncSequence, completion handlers, locks
references/interop.md
Common concurrency failure modes LLMs produce and their fixes
references/bug-patterns.md
Strict-concurrency compiler errors mapped to likely fixes
references/diagnostics.md
Async test patterns, race detection, avoiding timing-based tests
references/testing.md
主题参考文件
代码审查的Grep目标——已知危险模式
references/hotspots.md
Swift 6.2:默认隔离、
@concurrent
Task.immediate
、隔离析构函数
references/new-features.md
Actor可重入性、全局Actor推断、隔离模式
references/actors.md
任务组、
async let
withDiscardingTaskGroup
、并发限制
references/structured.md
Task
vs
Task.detached
Task {}
何时属于代码异味
references/unstructured.md
协作式取消、
withTaskCancellationHandler
、有问题的模式
references/cancellation.md
AsyncStream.makeStream(of:)
、continuation生命周期、背压
references/async-streams.md
受检continuation、包装委托、
@unchecked Sendable
references/bridging.md
GCD迁移、Combine转AsyncSequence、完成处理程序、锁
references/interop.md
大语言模型生成的常见并发故障模式及其修复方案
references/bug-patterns.md
严格并发编译器错误与可能修复方案的映射
references/diagnostics.md
异步测试模式、竞态检测、避免基于时序的测试
references/testing.md