Install any skill in seconds. Free to start, no credit card required.
Get Started Free →xu-xiang/swift-concurrency-6-2
Swift 6.2 易用的并发(Approachable Concurrency)—— 默认单线程,使用 @concurrent 进行显式后台卸载,针对主执行角色(Main Actor)类型的隔离一致性。
Passed 22 of 22 test cases with the skill, and 13 of 22 without it.
and 11 more in the full report
9 fixed13 passed either way0 still failing0 broke
Tokens, turns and time describe resource use, not a dollar price.
One of 9 test cases the skill fixed. Same model, same prompt, with and without the skill.
我想在某个类内部对深层数据结构进行复杂压缩计算,希望用 `@concurrent` 关键字修饰此计算方法。请列出在 Swift 6.2 中成功声明并调用该 `@concurrent` 方法所必须满足的 4 个步骤规范。
| Checked | Without skill | With skill |
|---|---|---|
| Specifies marking the containing type as `nonisolated`. | Missed | Passed |
| Specifies adding the `async` keyword to the `@concurrent` function signature if not already present. | Missed | Passed |
The instructions your model reads when the skill loads.
采用 Swift 6.2 并发模型的模式:代码默认在单线程运行,并发需显式引入。在不牺牲性能的情况下消除常见的数据竞争(Data-race)错误。
MainActor 的应用架构MainActor 隔离的类型上实现协议一致性(Protocol conformances)在 Swift 6.1 及更早版本中,异步函数(Async functions)可能会被隐式卸载到后台线程,即使在看似安全的代码中也会导致数据竞争错误:
// Swift 6.1: 错误 (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 }
// 错误:发送 'self.photoProcessor' 存在引起数据竞争的风险
return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
}
}
Swift 6.2 修复了这个问题:异步函数默认保留在调用方执行角色(Actor)上。
// Swift 6.2: 正常 (OK) — 异步保留在 MainActor,无数据竞争
@MainActor
final class StickerModel {
let photoProcessor = PhotoProcessor()
test cases passed, vs 13 / 22 without
Published skills whose descriptions closely match this one, with their headline benchmark lift.
More skills from the same source and nearby in the registry, with their headline benchmark lift.