swift-concurrency-expert by dimillian/skills
npx skills add https://github.com/dimillian/skills --skill swift-concurrency-expert通过应用 Actor 隔离、Sendable 安全性以及现代并发模式,以最小的行为变更来审查和修复 Swift 6.2+ 代码库中的 Swift 并发问题。
@MainActor、actor、nonisolated)以及是否启用了默认 Actor 隔离模式。优先选择在满足数据竞争安全性的同时保留现有行为的编辑方案。
常见修复方法:
@MainActor 注解类型或相关成员。extension Foo: @MainActor SomeProtocol)。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@MainActornonisolated 类型上的 @concurrent 异步函数中,或使用 actor 来保护可变状态。Sendable 一致性;除非能证明线程安全,否则避免使用 @unchecked Sendable。UI 绑定类型 — 添加 @MainActor
// 之前:数据竞争警告,因为 ViewModel 在主线程上被访问,但没有 Actor 隔离
class ViewModel: ObservableObject {
@Published var title: String = ""
func load() { title = "Loaded" }
}
// 之后:注解整个类型,使所有存储状态和方法自动隔离到主 Actor
@MainActor
class ViewModel: ObservableObject {
@Published var title: String = ""
func load() { title = "Loaded" }
}
协议一致性隔离
// 之前:编译器错误 — SomeProtocol 方法是非隔离的,但遵循的类型是 @MainActor
@MainActor
class Foo: SomeProtocol {
func protocolMethod() { /* 访问主 Actor 状态 */ }
}
// 之后:将一致性限定为 @MainActor,以便在正确的隔离上下文中满足要求
@MainActor
extension Foo: SomeProtocol {
func protocolMethod() { /* 安全地访问主 Actor 状态 */ }
}
使用 @concurrent 的后台工作
// 之前:耗时的计算阻塞了主 Actor
@MainActor
func processData(_ input: [Int]) -> [Int] {
input.map { heavyTransform($0) } // 在主线程上运行
}
// 之后:将繁重工作移出主 Actor,然后返回结果
// 调用方等待结果并停留在其自己的 Actor 上
nonisolated func processData(_ input: [Int]) async -> [Int] {
await Task.detached(priority: .userInitiated) {
input.map { heavyTransform($0) }
}.value
}
// 或者,使用 @concurrent 异步函数(Swift 6.2+):
@concurrent
func processData(_ input: [Int]) async -> [Int] {
input.map { heavyTransform($0) }
}
references/swift-6-2-concurrency.md 了解 Swift 6.2 的变更、模式和示例。references/approachable-concurrency.md。references/swiftui-concurrency-tour-wwdc.md 获取特定于 SwiftUI 的并发指导。每周安装量
664
代码仓库
GitHub 星标数
2.3K
首次出现
2026年1月20日
安全审计
安装于
codex512
claude-code498
opencode491
gemini-cli471
cursor432
github-copilot396
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.
@MainActor, actor, nonisolated) and whether a default actor isolation mode is enabled.Prefer edits that preserve existing behavior while satisfying data-race safety.
Common fixes:
@MainActor.extension Foo: @MainActor SomeProtocol).@MainActor or move into an actor.@concurrent async function on a nonisolated type or use an actor to guard mutable state.Sendable conformance only when correct; avoid @unchecked Sendable unless you can prove thread safety.UI-bound type — adding@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" }
}
Protocol conformance isolation
// 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 */ }
}
Background work with@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) }
}
references/swift-6-2-concurrency.md for Swift 6.2 changes, patterns, and examples.references/approachable-concurrency.md when the project is opted into approachable concurrency mode.references/swiftui-concurrency-tour-wwdc.md for SwiftUI-specific concurrency guidance.Weekly Installs
664
Repository
GitHub Stars
2.3K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex512
claude-code498
opencode491
gemini-cli471
cursor432
github-copilot396
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装