Persist 是用 Swift 编写的简单、可重复使用的 Core Data 栈。它还提供了一些轻量级的辅助函数,用于常见的操作,如创建实体和查询。
它支持 iOS 8+、watchOS 2.0 和 Mac OS 10.9+。
Persist 使用简单的父子 Core Data 栈,其中有一个私有写入上下文直接附加到 NSPersistentStoreCoordinator,有一个主队列上下文作为其子上下文暴露给用户。
┌─────────────────────────────────────────────────────────────────────────┐
│ ┌─────────────────────────────────┐ │
│ │ Persistent Store Coordinator │ │
│ └────────────────┬────────────────┘ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ │
│ │ Private Queue Context │ │
│ └────────────────┬────────────────┘ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ ┌─────────────────┐ │
│ │ Main Queue Context ├─────────▶ User Interface │ │
│ └────────────────┬────────────────┘ └─────────────────┘ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ ┌────────────────────────┐ │
│ │ Temporary Private Queue Context ◀─────────┤ Writes/Updates │ │
│ └─────────────────────────────────┘ └────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
Persist 提供了通过 StoreType
枚举对其底层存储配置的完全访问,提供了安全地构建 InMemory、SQLite 和 Binary 存储类型的构建,并允许您提供自定义存储类型。
创建内存存储看起来像这样
import Persist
let bundle = NSBundle.mainBundle()
let modelPath = bundle.pathForResource("MyModel", ofType: "momd")!
let modelURL = NSURL(fileURLWithPath: modelPath)
let persist = try! Persist(storeType: .InMemory, modelURL: modelURL)
您可以在 PersistTests/PersistStoreBasedTests.swift 中看到更多示例
要选择您的 NSManagedObject 子类使用辅助函数,请用如下方式对它们进行注释
@objc(Person)
class Person: ManagedObjectType {
}
如果您没有为您的类提供 Objective-C 名称,或者更新您的模型配置以支持模块命名空间,您需要提供自己的 static func entityName() -> String
实现。
欢迎提出问题和功能请求,尽管目标是保持 Persist 轻量级。
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)