关于 concurrency-expert
concurrency-expert 技能用于审查和修复 Swift 6.2+ 代码库中的并发问题。它运用 Actor 隔离、Sendable 安全性和现代并发模式,并尽量保持现有行为。
主要功能
- 01分析编译器诊断与 Actor 上下文
- 02为 UI 代码应用 Actor 隔离
- 03保护共享状态和全局状态
- 04将工作移出主 Actor
- 05指导 Swift 并发迁移
适用场景
- 01解决严格并发检查错误
- 02安全隔离 UI 相关类型
- 03将后台处理移出主 Actor
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ patrickserrano/ skills --skill concurrency-expert安装后在 Claude Code 中输入/concurrency-expert确认已加载。
详细介绍
Swift 并发专家
概览
通过应用 Actor 隔离、Sendable 安全性和现代并发模式,以尽可能少地改变行为,审查并修复 Swift 6.2+ 代码库中的 Swift 并发问题。
工作流
1. 排查问题
- 记录准确的编译器诊断信息和相关符号
- 检查项目并发设置:Swift 语言版本(6.2+)、严格并发级别
- 检查是否启用了易用并发(默认 Actor 隔离/默认主 Actor 隔离)
- 确认当前 Actor 上下文(
@MainActor、actor、nonisolated) - 确认代码是否与 UI 绑定,或是否预期在主 Actor 之外运行
2. 应用最小安全修复
优先选择既能保留现有行为,又能满足数据竞争安全要求的修改。
常见修复:
| 问题 | 修复 |
|---|---|
| 与 UI 绑定的类型 | 为类型或成员添加 @MainActor 注解 |
| 主 Actor 类型上的协议遵循 | 将遵循声明为隔离:extension Foo: @MainActor SomeProtocol |
| 全局/静态状态 | 使用 @MainActor 保护,或将其移入 Actor |
| 后台工作 | 在 nonisolated 类型中使用 @concurrent 异步函数 |
| Sendable 错误 | 优先使用不可变/值类型;仅在确实合适时添加 Sendable |
Swift 6.2 的关键变化
默认 Actor 隔离
Swift 6.2 默认保持单线程,直到你选择引入并发:
// In Swift 6.2 with approachable concurrency enabled,
// this no longer produces a data race error
@MainActor
final class StickerModel {
let photoProcessor = PhotoProcessor()
func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? {
guard let data = try await item.loadTransferable(type: Data.self) else {
return nil
}
// Safe - runs on main actor by default
return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
}
}
隔离遵循
现在支持需要主 Actor 状态的遵循:
protocol Exportable {
func export()
}
// Isolated conformance - safe because compiler ensures
// it's only used on the main actor
extension StickerModel: @MainActor Exportable {
func export() {
photoProcessor.exportAsPNG()
}
}
保护全局状态
// Protect with @MainActor
@MainActor
final class StickerLibrary {
static let shared: StickerLibrary = .init()
}
// Or enable main-actor-by-default mode for the whole project
将工作转移到后台
使用 @concurrent,显式地在并发线程池上运行代码:
nonisolated struct PhotoProcessor {
@concurrent
func process(data: Data) async -> ProcessedPhoto? {
// Runs on background thread
}
}
// Caller adds await
processedPhotos[item.id] = await PhotoProcessor().process(data: data)
迁移检查清单
- 检查构建设置中的 Swift 版本(需要 6.2+)
- 在构建设置中启用易用并发功能
- 运行 Swift 迁移工具:
swift.org/migration - 使用上述模式修复剩余的编译器错误
- 彻底测试——并发错误可能会在运行时显现
常见模式
与 UI 绑定的类
@MainActor
final class ViewModel {
var items: [Item] = []
func load() async {
items = try await service.fetchItems()
}
}
后台处理
nonisolated struct ImageProcessor {
@concurrent
static func resize(_ image: UIImage, to size: CGSize) async -> UIImage {
// Heavy work runs off main actor
}
}
用于共享可变状态的 Actor
actor Cache {
private var storage: [String: Data] = [:]
func get(_ key: String) -> Data? {
storage[key]
}
func set(_ key: String, value: Data) {
storage[key] = value
}
}
构建设置
在 Xcode 的 Swift Compiler - Concurrency 下启用:
SWIFT_STRICT_CONCURRENCY= complete- 易用并发功能(Swift 6.2+)
或者在 Package.swift 中:
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
评论
支持 Markdown