swift-actor-persistence
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwift Actors for Thread-Safe Persistence
基于Swift Actors的线程安全持久化
Lifecycle Position
生命周期定位
Phase 3 (Implement). Load when building local data storage, offline-first patterns, or caching layers. Related: for general actor patterns, for server sync.
swift-concurrencyswift-networking阶段3(实现)。在构建本地数据存储、离线优先模式或缓存层时加载。相关内容:(通用Actor模式)、(服务器同步)。
swift-concurrencyswift-networkingWhen to Use
使用场景
- Building a local data persistence layer
- Need thread-safe access to shared mutable state with disk backing
- Want compile-time data race elimination (no manual locks or queues)
- Building offline-first apps with local storage
- Replacing legacy -based file managers
DispatchQueue
- 构建本地数据持久化层
- 需要对带磁盘存储的共享可变状态进行线程安全访问
- 希望通过编译时消除数据竞争(无需手动锁或队列)
- 构建带本地存储的离线优先应用
- 替换基于的传统文件管理器
DispatchQueue
Core Pattern: Actor-Based Repository
核心模式:基于Actor的仓库
The actor model guarantees serialized access — no data races, enforced by the compiler.
swift
public actor LocalRepository<T: Codable & Identifiable> where T.ID: Hashable & Codable {
private var cache: [T.ID: T] = [:]
private let fileURL: URL
public init(directory: URL = .documentsDirectory, filename: String = "data.json") {
self.fileURL = directory.appendingPathComponent(filename)
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: T.ID) throws {
cache[id] = nil
try persistToFile()
}
public func find(by id: T.ID) -> 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) -> [T.ID: 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) })
}
}All calls are automatically async due to actor isolation:
swift
let repo = LocalRepository<Question>(filename: "questions.json")
let question = await repo.find(by: questionID) // O(1) cache lookup
let all = await repo.loadAll()
try await repo.save(newQuestion) // updates cache + persists
try await repo.delete(questionID)Actor模型保证序列化访问——由编译器强制执行,不会出现数据竞争。
swift
public actor LocalRepository<T: Codable & Identifiable> where T.ID: Hashable & Codable {
private var cache: [T.ID: T] = [:]
private let fileURL: URL
public init(directory: URL = .documentsDirectory, filename: String = "data.json") {
self.fileURL = directory.appendingPathComponent(filename)
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: T.ID) throws {
cache[id] = nil
try persistToFile()
}
public func find(by id: T.ID) -> 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) -> [T.ID: 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隔离自动变为异步:
swift
let repo = LocalRepository<Question>(filename: "questions.json")
let question = await repo.find(by: questionID) // O(1) 缓存查找
let all = await repo.loadAll()
try await repo.save(newQuestion) // 更新缓存 + 持久化
try await repo.delete(questionID)Combining with @Observable ViewModel
与@Observable ViewModel结合
swift
@Observable @MainActor
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()
}
func remove(_ id: Question.ID) async throws {
try await repository.delete(id)
questions = await repository.loadAll()
}
}swift
@Observable @MainActor
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()
}
func remove(_ id: Question.ID) async throws {
try await repository.delete(id)
questions = await repository.loadAll()
}
}Design Decisions
设计决策
| Decision | Rationale |
|---|---|
| Actor (not class + lock) | Compiler-enforced thread safety, zero manual synchronization |
| In-memory cache + file persistence | Fast reads from cache, durable writes to disk |
| Synchronous init loading | Avoids async initialization complexity; actor isolation not yet active during |
| Dictionary keyed by ID | O(1) lookups by identifier |
Generic over | Reusable across any model type |
Atomic file writes ( | Prevents partial writes on crash |
| 决策 | 理由 |
|---|---|
| 使用Actor(而非类+锁) | 编译器强制执行线程安全,无需手动同步 |
| 内存缓存+文件持久化 | 从缓存快速读取,写入磁盘保证持久化 |
| 同步初始化加载 | 避免异步初始化复杂度;初始化期间Actor隔离尚未生效 |
| 按ID作为字典键 | 通过标识符实现O(1)查找 |
泛型约束 | 可在任何模型类型中复用 |
原子文件写入( | 崩溃时避免部分写入 |
Swift 6.2+ Considerations
Swift 6.2+ 注意事项
What Works Well
适用场景
- Actor isolation is fully enforced at compile time — Swift 6 strict concurrency mode catches all data race violations. This pattern is the recommended replacement for -based synchronization.
DispatchQueue - Synchronous init is safe — Actor designated initializers can access stored properties synchronously because no other code can reference until init completes. The
selfstatic method pattern is valid.loadSynchronously - Generic actors are fully supported.
- Actor隔离在编译时完全强制执行——Swift 6严格并发模式可捕获所有数据竞争违规。该模式是基于同步方案的推荐替代方案。
DispatchQueue - 同步初始化安全——Actor指定初始化器可同步访问存储属性,因为初始化完成前其他代码无法引用。
self静态方法模式是有效的。loadSynchronously - 泛型Actor完全支持。
Adaptations Made (vs. Original)
适配调整(与原始版本对比)
-
on ViewModels — The original used
@Observable @MainActorwithout@Observable final class. In Swift 6 strict concurrency, ViewModels accessed from SwiftUI views must be@MainActor-isolated to avoid actor-boundary crossing errors.@MainActor -
instead of
T.ID: Hashable & Codable— The original constrained ID toT.ID == String. Relaxing toStringsupportsHashable & Codable,UUID, and custom ID types used in real-world Swift apps.Int
-
ViewModel使用——原始版本使用
@Observable @MainActor但未添加@Observable final class。在Swift 6严格并发模式下,SwiftUI视图访问的ViewModel必须处于@MainActor隔离状态,以避免跨Actor边界错误。@MainActor -
****替代
T.ID: Hashable & Codable——原始版本将ID约束为T.ID == String。放宽为String支持Hashable & Codable、UUID以及实际Swift应用中使用的自定义ID类型。Int
Known Trade-offs
已知权衡
Synchronous file I/O inside actors: and perform blocking disk I/O on the cooperative thread pool. For small files (< 1 MB) this is acceptable. For large datasets, consider:
Data.write(to:)Data(contentsOf:)swift
// Option 1: Offload to a non-cooperative thread
private func persistToFile() async throws {
let data = try JSONEncoder().encode(Array(cache.values))
let url = fileURL
try await Task.detached {
try data.write(to: url, options: .atomic)
}.value
}
// Option 2: Use SwiftData or Core Data for large datasets
// This pattern is best suited for lightweight local storageNo migration support: This is raw JSON file storage. If your model evolves, consider adding a version field:
swift
private struct VersionedStore<T: Codable>: Codable {
let version: Int
let items: [T]
}Actor内部的同步文件I/O:和在协作线程池上执行阻塞磁盘I/O。对于小文件(<1MB),这是可接受的。对于大型数据集,可考虑:
Data.write(to:)Data(contentsOf:)swift
// 选项1:卸载到非协作线程
private func persistToFile() async throws {
let data = try JSONEncoder().encode(Array(cache.values))
let url = fileURL
try await Task.detached {
try data.write(to: url, options: .atomic)
}.value
}
// 选项2:对大型数据集使用SwiftData或Core Data
// 该模式最适合轻量级本地存储无迁移支持:这是原始JSON文件存储。如果模型演进,考虑添加版本字段:
swift
private struct VersionedStore<T: Codable>: Codable {
let version: Int
let items: [T]
}Anti-Patterns
反模式
| Don't | Do Instead |
|---|---|
| Use actors — compiler-enforced, zero runtime overhead |
| Expose the internal cache dictionary | Only expose domain operations ( |
| Defeats the purpose — rethink the design |
| Call actor methods in a tight loop | Batch operations into a single actor method |
| Use for datasets > 10 MB | Switch to SwiftData, Core Data, or SQLite |
| 不要做 | 替代方案 |
|---|---|
使用 | 使用Actors——编译器强制执行,无运行时开销 |
| 暴露内部缓存字典 | 仅暴露领域操作( |
使用 | 违背设计初衷——重新考虑设计 |
| 在循环中频繁调用Actor方法 | 将操作批量处理为单个Actor方法 |
| 用于大于10MB的数据集 | 切换到SwiftData、Core Data或SQLite |
Cross-References
交叉引用
- — General actor patterns, reentrancy,
swift-concurrency, GCD migration@MainActor - — Async network calls that feed data into actor-based repositories
swift-networking - — Scene phase changes for triggering persistence saves
swift-app-lifecycle
- ——通用Actor模式、可重入性、
swift-concurrency、GCD迁移@MainActor - ——将数据输入基于Actor的仓库的异步网络调用
swift-networking - ——触发持久化保存的场景阶段变化
swift-app-lifecycle
Templates
模板
SwiftData persistence layer in — copy and adapt:
templates/- — Generic
Repository.swiftprotocol for CRUD operationsRepository - —
SwiftDataRepository.swiftSwiftData implementation with batch operations and pagination@MainActor - — Sample
ExampleModel.swiftentity showing SwiftData patterns@Model - —
PersistenceController.swiftsetup with migration supportModelContainer
templates/- ——用于CRUD操作的通用
Repository.swift协议Repository - ——带批量操作和分页的
SwiftDataRepository.swiftSwiftData实现@MainActor - ——展示SwiftData模式的示例
ExampleModel.swift实体@Model - ——带迁移支持的
PersistenceController.swift设置ModelContainer