关于 swift-concurrency-expert
面向 Swift 6.2+ 代码库的并发问题审查与修复技能,供 Swift 开发者按编译诊断定位问题,并以尽量少的行为改动验证安全修复。
主要功能
- 01排查编译诊断与并发设置
- 02应用隔离与 Sendable 修复
- 03将后台工作移出主执行器
- 04重新构建并测试回归
适用场景
- 01解决 Swift 6.2+ 严格并发错误
- 02安全隔离 UI 状态
- 03处理 Sendable 警告与可变全局状态
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dimillian/ skills --skill swift-concurrency-expert安装后在 Claude Code 中输入/swift-concurrency-expert确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
agents/openai.yamlreferences/approachable-concurrency.mdswift-6-2-concurrency.mdswiftui-concurrency-tour-wwdc.md详细介绍
Swift 并发专家
概述
通过应用 Actor 隔离、Sendable 安全和现代并发模式,在尽量不改变行为的前提下,检查并修复 Swift 6.2 及更高版本代码库中的 Swift 并发问题。
工作流
1. 分类问题
- 记录准确的编译器诊断信息和出问题的符号。
- 检查项目的并发设置:Swift 语言版本(6.2 及更高版本)、严格并发级别,以及是否启用了易用并发(默认 Actor 隔离/默认主 Actor 隔离)。
- 确定当前的 Actor 上下文(
@MainActor、actor、nonisolated),以及是否启用了默认 Actor 隔离模式。 - 确认代码是否与 UI 绑定,或是否旨在主 Actor 之外运行。
2. 应用最小且安全的修复
优先选择在满足数据竞争安全要求的同时保留现有行为的修改。
常见修复:
- 与 UI 绑定的类型:为类型或相关成员标注
@MainActor。 - 主 Actor 类型上的协议遵循:使遵循具备隔离性(例如,
extension Foo: @MainActor SomeProtocol)。 - 全局/静态状态:使用
@MainActor保护,或将其移入 Actor。 - 后台工作:将耗时工作移入
nonisolated类型上的@concurrent异步函数,或使用actor保护可变状态。 - Sendable 错误:优先使用不可变/值类型;仅在正确的情况下添加
Sendable遵循;除非能证明线程安全,否则避免使用@unchecked Sendable。
3. 验证修复
- 重新构建,确认所有并发诊断均已解决,且没有引入新的警告。
- 运行测试套件以检查回归问题——即使构建通过,并发修改也可能引入细微的运行时问题。
- 如果修复引发了新的警告,请将每个警告视为新的分类任务(返回第 1 步),并迭代解决,直到构建干净且测试通过。
示例
与 UI 绑定的类型——添加 @MainActor
// Before: data-race warning because ViewModel is accessed from the main thread
// but has no actor isolation
class ViewModel: ObservableObject {
@Published var title: String = ""
func load() { title = "Loaded" }
}
// After: annotate the whole type so all stored state and methods are
// automatically isolated to the main actor
@MainActor
class ViewModel: ObservableObject {
@Published var title: String = ""
func load() { title = "Loaded" }
}
协议遵循隔离
// Before: compiler error — SomeProtocol method is nonisolated but the
// conforming type is @MainActor
@MainActor
class Foo: SomeProtocol {
func protocolMethod() { /* accesses main-actor state */ }
}
// After: scope the conformance to @MainActor so the requirement is
// satisfied inside the correct isolation context
@MainActor
extension Foo: SomeProtocol {
func protocolMethod() { /* safely accesses main-actor state */ }
}
使用 @concurrent 执行后台工作
// Before: expensive computation blocks the main actor
@MainActor
func processData(_ input: [Int]) -> [Int] {
input.map { heavyTransform($0) } // runs on main thread
}
// After: hop off the main actor for the heavy work, then return the result
// The caller awaits the result and stays on its own actor
nonisolated func processData(_ input: [Int]) async -> [Int] {
await Task.detached(priority: .userInitiated) {
input.map { heavyTransform($0) }
}.value
}
// Or, using a @concurrent async function (Swift 6.2+):
@concurrent
func processData(_ input: [Int]) async -> [Int] {
input.map { heavyTransform($0) }
}
参考资料
- 有关 Swift 6.2 的变更、模式和示例,请参阅
references/swift-6-2-concurrency.md。 - 如果项目启用了易用并发模式,请参阅
references/approachable-concurrency.md。 - 有关 SwiftUI 特定的并发指南,请参阅
references/swiftui-concurrency-tour-wwdc.md。
评论
支持 Markdown