swift-actor-persistencelisted
Install: claude install-skill majiayu000/claude-skill-registry-data
# Swift Actors for Thread-Safe Persistence
# スレッドセーフな永続化のためのSwift Actor
**Extracted / 抽出日:** 2026-02-05
**Context / コンテキスト:** iOS/Swift apps requiring thread-safe data persistence with async/await
async/awaitを使用したスレッドセーフなデータ永続化が必要なiOS/Swiftアプリ
---
## Problem / 課題
Data persistence layers often face race conditions when multiple parts of an app read/write simultaneously. Traditional approaches (DispatchQueues, locks) are error-prone and verbose.
データ永続化レイヤーは、アプリの複数の部分が同時に読み書きする際にレースコンディションに直面することが多い。従来のアプローチ(DispatchQueues、ロック)はエラーが発生しやすく、冗長になりがち。
---
## Solution / 解決策
Use Swift actors to isolate all persistence state and operations. The actor model guarantees:
- No data races (compiler-enforced)
- Automatic serialization of access
- Async-first API that integrates with structured concurrency
Swift actorを使用して、すべての永続化状態と操作を分離する。actorモデルは以下を保証:
- データ競合なし(コンパイラによる強制)
- アクセスの自動シリアライズ
- 構造化並行性と統合されたasyncファーストAPI
```swift
public actor LocalRepository {
private var cache: [String: Record] = [:]
private let cacheFileURL: URL
public init(directory: URL = .documentsDirectory) {
self.cacheFileURL = directory.appendingPathComponent("cache.json")
// Synchronous cache load during init (actor isolation not yet active)
// init中の同期キャッシュ読み込み(actor分離がまだアクティブでないため)
self.cache = Self.loadCacheSynchronously(from: cacheFileURL)
}
public func save(_ record: Record) throws {
cache[record.id] = record
try persistToFile()