swift-actor-persistence
Swift Actor Persistence demonstrates how to construct a thread-safe persistence layer by combining actor isolation with hybrid storage—keeping frequently accessed data in memory while syncing changes to disk. The pattern leverages Swift's compile-time guarantees to prevent data races without manual locks, making it ideal for offline-first applications and shared mutable state across concurrent contexts.
Swift Actor Persistence enables you to build thread-safe data storage using Swift actors combined with memory caching and file persistence.
AI-generated summary based on this skill's SKILL.md
Install
xu-xiang/everything-claude-code-zh/swift-actor-persistence · repository language: JavaScript
git clone https://github.com/xu-xiang/everything-claude-code-zh
cp -r everything-claude-code-zh/skills/swift-actor-persistence ~/.claude/skills/swift-actor-persistenceFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What is swift actor thread safe persistence?
Swift Actor Persistence is a pattern that uses Swift actors to build thread-safe data storage without manual locks. It combines in-memory caching with file-based persistence, ensuring safe concurrent access through actor isolation. The approach leverages Swift's compile-time concurrency guarantees to prevent data races in shared mutable state, making it ideal for offline-first applications.
How does Swift Actor Persistence replace locks and DispatchQueue?
Swift Actor Persistence eliminates the need for DispatchQueue and manual locking by using actor-based serialization. Actors automatically serialize access to their mutable state, guaranteeing that only one task can execute at a time. This removes the complexity of managing locks while providing stronger compile-time safety guarantees than traditional concurrency patterns.
Can I combine memory caching with file-based persistence safely?
Yes. Swift Actor Persistence demonstrates hybrid storage by keeping frequently accessed data in memory while syncing changes to disk. The actor model ensures that both cache and file operations remain synchronized and thread-safe. This pattern is particularly effective for offline-first apps where local data must be reliably persisted and quickly accessible.
What is the swift actor based repository pattern?
Swift Actor Persistence implements a repository pattern where an actor manages all data access—both reading from and writing to storage. The actor isolates mutable state, handles serialized file I/O, and coordinates between memory cache and persistent storage. This centralizes concurrency concerns and eliminates data races across your application.
How does Swift Actor Persistence eliminate data races?
Swift Actor Persistence prevents data races through actor isolation and Sendable constraints. Actors serialize all access to shared mutable state, ensuring only one concurrent task modifies data at a time. Combined with Swift's type-safe Sendable protocol for data crossing actor boundaries, the pattern provides compile-time guarantees that eliminate race conditions.
Is Swift Actor Persistence suitable for offline-first apps?
Yes. Swift Actor Persistence is designed for offline-first applications by providing reliable local storage with thread-safe concurrent access. The hybrid memory-and-file approach ensures data persists across app sessions while remaining quickly accessible. The actor model guarantees safe concurrent reads and writes, essential for apps that must work reliably without network connectivity.
SKILL.md
rendered from the published skill — quoted content, verbatim
使用 Swift Actors 实现线程安全持久化 (Swift Actors for Thread-Safe Persistence)
使用 Swift actor 构建线程安全数据持久化层的模式。该模式结合了内存缓存(In-memory caching)与基于文件的存储,利用 actor 模型(Actor model)在编译时消除数据竞争(Data races)。
何时启用
- 在 Swift 5.5+ 中构建数据持久化层
- 需要对共享可变状态(Shared mutable state)进行线程安全访问
- 希望消除手动同步操作(如 locks、DispatchQueues)
- 构建具有本地存储的离线优先(Offline-first)应用
核心模式 (Core Pattern)
基于 Actor 的仓库模式 (Actor-Based Repository)
Actor 模型保证了序列化访问(Serialized access)—— 由编译器强制执行,确保不会出现数据竞争。
```swift public 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)
// 在 init 期间同步加载(此时 actor 隔离尚未生效)
self.cache = Self.loadSynchronously(from: fileURL)
}
// MARK: - 公共 API
public func save(_ item: T) throws {
cache[item.id] = item
try persistToFile()
}
public func delete(_ id: String) throws {
cache[id]
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/swift-actor-persistence/SKILL.md