Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design.
.claude/skills/affaan-m-swift-actor-persistence/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -17% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 8% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 41% | 0% |
| case-13 | ✗→✓ | ▲ Improved | 16% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 19% | 0% |
使用 Swift actor 构建线程安全数据持久化层的模式。结合内存缓存与文件支持的存储,利用 actor 模型在编译时消除数据竞争。
Actor 模型保证了序列化访问 —— 没有数据竞争,由编译器强制执行。
swiftpublic actor LocalRepository<T: Codable & Identifiable> where T.ID == String { private var cache: [String: T] = [:] private let fileURL: URL public init(directory: URL = .documentsDirectory, filename: String = "data.json") { self.fileURL = directory.appendingPathComponent(filename) // Synchronous load during init (actor isolation not yet active) self.cache = Self.loadSynchronously(from: fileURL) } // MARK: - Public API public func save(_ item: T) throws { cache[item.id] = item try persistToFile() } public func delete(_ id: String) throws { cache[id] = nil try persistToFile() } public func find(by id: String) -> T? { cache[id] } public func loadAll() -> [T] { Array(cache.values) } // MARK: - Private private func persistToFile() throws { let data = try JSONEncoder().encode(Array(cache.values)) try data.write(to: fileURL, options: .atomic) } private static func loadSynchronously(from url: URL) -> [String: T] { guard let data = try? Data(contentsOf: url), let items = try? JSONDecoder().decode([T].self, from: data) else { return [:] } return Dictionary(uniqueKeysWithValues: items.map { ($0.id, $0) }) } }
由于 actor 隔离,所有调用都会自动变为异步:
swiftlet repository = LocalRepository<Question>() // Read — fast O(1) lookup from in-memory cache let question = await repository.find(by: "q-001") let allQuestions = await repository.loadAll() // Write — updates cache and persists to file atomically try await repository.save(newQuestion) try await repository.delete("q-001")
swift@Observable final class QuestionListViewModel { private(set) var questions: [Question] = [] private let repository: LocalRepository<Question> init(repository: LocalRepository<Question> = LocalRepository()) { self.repository = repository } func load() async { questions = await repository.loadAll() } func add(_ question: Question) async throws { try await repository.save(question) questions = await repository.loadAll() } }
| 决策 | 理由 | |----------|-----------| | Actor(而非类 + 锁) | 编译器强制执行的线程安全性,无需手动同步 | | 内存缓存 + 文件持久化 | 从缓存中快速读取,持久化写入磁盘 | | 同步初始化加载 | 避免异步初始化的复杂性 | | 按 ID 键控的字典 | 按标识符进行 O(1) 查找 | | 泛型化 Codable & Identifiable | 可在任何模型类型中重复使用 | | 原子文件写入 (.atomic) | 防止崩溃时部分写入 |
Sendable 类型.atomic 写入 以防止应用在写入过程中崩溃导致数据损坏init 中同步加载 —— 异步初始化器会增加复杂性,而对本地文件的益处微乎其微@Observable ViewModel 结合使用 以实现响应式 UI 更新DispatchQueue 或 NSLock 而非 actorawait —— 调用者必须处理异步上下文nonisolated 来绕过 actor 隔离(违背了初衷)DispatchQueue 的旧式线程安全机制| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-08 | pass→pass | 15,243 | 14,877 | -2% | 1 | 1 | 0% | 2,633 | 3,757 | +43% | 0 | 0 | — |
case-09 | pass→pass | 12,751 | 9,216 | -28% | 1 | 1 | 0% | 2,226 | 2,833 | +27% | 0 | 0 | — |
case-01 | fail→pass | 19,967 | 11,027 | -45% | 1 | 1 | 0% | 4,172 | 3,475 | -17% | 0 | 0 | — |
case-02 | pass→pass | 18,164 | 16,203 | -11% | 1 | 1 | 0% | 3,717 | 5,191 | +40% | 0 | 0 | — |
case-03 | fail→pass | 18,674 | 13,173 | -29% | 1 | 1 | 0% | 3,950 | 4,255 | +8% | 0 | 0 | — |
case-04 | pass→pass | 11,797 | 8,182 | -31% | 1 | 1 | 0% | 2,235 | 2,660 | +19% | 0 | 0 | — |
case-05 | pass→pass | 13,843 | 13,130 | -5% | 1 | 1 | 0% | 2,916 | 3,943 | +35% | 0 | 0 | — |
case-06 | fail→fail | 14,555 | 11,461 | -21% | 1 | 1 | 0% | 2,329 | 3,212 | +38% | 0 | 0 | — |
case-07 | fail→pass | 12,037 | 11,125 | -8% | 1 | 1 | 0% | 2,335 | 3,298 | +41% | 0 | 0 | — |
case-10 | fail→fail | 12,202 | 5,347 | -56% | 1 | 1 | 0% | 2,417 | 2,142 | -11% | 0 | 0 | — |
case-11 | pass→pass | 6,855 | 4,854 | -29% | 1 | 1 | 0% | 1,183 | 2,150 | +82% | 0 | 0 | — |
case-12 | pass→pass | 13,534 | 7,342 | -46% | 1 | 1 | 0% | 2,289 | 2,501 | +9% | 0 | 0 | — |
case-13 | fail→pass | 13,627 | 6,968 | -49% | 1 | 1 | 0% | 2,172 | 2,524 | +16% | 0 | 0 | — |
case-14 | fail→pass | 9,089 | 4,871 | -46% | 1 | 1 | 0% | 1,724 | 2,060 | +19% | 0 | 0 | — |
case-15 | fail→pass | 15,613 | 10,765 | -31% | 1 | 1 | 0% | 2,845 | 3,237 | +14% | 0 | 0 | — |
case-16 | pass→pass | 10,940 | 5,497 | -50% | 1 | 1 | 0% | 1,996 | 2,256 | +13% | 0 | 0 | — |
case-17 | fail→pass | 13,870 | 10,612 | -23% | 1 | 1 | 0% | 2,437 | 3,201 | +31% | 0 | 0 | — |
case-18 | fail→fail | 15,470 | 12,720 | -18% | 1 | 1 | 0% | 2,819 | 3,678 | +30% | 0 | 0 | — |
case-19 | pass→pass | 16,687 | 14,315 | -14% | 1 | 1 | 0% | 3,615 | 4,213 | +17% | 0 | 0 | — |
case-20 | pass→pass | 10,146 | 10,798 | +6% | 1 | 1 | 0% | 2,120 | 3,571 | +68% | 0 | 0 | — |
case-21 | pass→pass | 12,974 | 13,643 | +5% | 1 | 1 | 0% | 2,802 | 4,198 | +50% | 0 | 0 | — |
case-22 | pass→pass | 13,413 | 13,606 | +1% | 1 | 1 | 0% | 2,938 | 4,277 | +46% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +32 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.