← ClaudeAtlas

swift-actor-persistencelisted

Use when building a thread-safe data persistence layer in Swift using actors with in-memory cache and file storage.
diegosouzapw/awesome-omni-skill · ★ 43 · Data & Documents · score 61
Install: claude install-skill diegosouzapw/awesome-omni-skill
# 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()