Swift Concurrency Expert
Attribution: copied from @Dimillian’s Dimillian/Skills (2025-12-31).
Overview
Review and fix Swift Concurrency issues in Swift 6.2+ codebases by applying actor isolation, Sendable safety, and modern concurrency patterns with minimal behavior changes.
Workflow
1. Triage the issue
- - Capture the exact compiler diagnostics and the offending symbol(s).
- Identify the current actor context (
@MainActor, actor, nonisolated) and whether a default actor isolation mode is enabled. - Confirm whether the code is UI-bound or intended to run off the main actor.
2. Apply the smallest safe fix
Prefer edits that preserve existing behavior while satisfying data-race safety.
Common fixes:
- - UI-bound types: annotate the type or relevant members with
@MainActor. - Protocol conformance on main actor types: make the conformance isolated (e.g.,
extension Foo: @MainActor SomeProtocol). - Global/static state: protect with
@MainActor or move into an actor. - Background work: move expensive work into a
@concurrent async function on a nonisolated type or use an actor to guard mutable state. - Sendable errors: prefer immutable/value types; add
Sendable conformance only when correct; avoid @unchecked Sendable unless you can prove thread safety.
Reference material
- - See
references/swift-6-2-concurrency.md for Swift 6.2 changes, patterns, and examples. - See
references/swiftui-concurrency-tour-wwdc.md for SwiftUI-specific concurrency guidance.
Swift 并发专家
归属:复制自 @Dimillian 的 Dimillian/Skills(2025-12-31)。
概述
通过应用 actor 隔离、Sendable 安全性和现代并发模式,以最小的行为变更审查并修复 Swift 6.2+ 代码库中的 Swift 并发问题。
工作流程
1. 分类问题
- - 捕获确切的编译器诊断信息和有问题的符号。
- 识别当前的 actor 上下文(@MainActor、actor、nonisolated)以及是否启用了默认 actor 隔离模式。
- 确认代码是绑定到 UI 还是计划在主 actor 之外运行。
2. 应用最小安全修复
优先选择既能保持现有行为又能满足数据竞争安全性的编辑。
常见修复:
- - UI 绑定类型:使用 @MainActor 注解类型或相关成员。
- 主 actor 类型上的协议遵循:使遵循隔离(例如 extension Foo: @MainActor SomeProtocol)。
- 全局/静态状态:使用 @MainActor 保护或移入 actor。
- 后台工作:将耗时工作移入 nonisolated 类型上的 @concurrent 异步函数,或使用 actor 保护可变状态。
- Sendable 错误:优先使用不可变/值类型;仅在正确时添加 Sendable 遵循;除非能证明线程安全,否则避免使用 @unchecked Sendable。
参考资料
- - 参见 references/swift-6-2-concurrency.md 了解 Swift 6.2 的变更、模式和示例。
- 参见 references/swiftui-concurrency-tour-wwdc.md 了解 SwiftUI 特定的并发指南。