JBPersistenceStore
[](https://travis-ci.org/Jan Bartel/JBPersistenceStore)
示例
要运行示例项目,请先克隆存储库,然后在 Example 目录中运行 pod install
要求
安装
JBPersistenceStore 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "JBPersistenceStore"
使用
模型
您希望持久化的模型必须实现 NSCoding 和 CanBePersistedProtocol。它们还必须继承自 NSObjet。
提示:您可以在扩展内部实现 CanBePersistedProtocol,以使文件更整洁。
public class DemoModel: NSObject, NSCoding {
(...)
}
extension DemoModel: CanBePersistedProtocol {
(...)
}
实现 CanBePersistedProtocol
要实现 CanBePersistedProtocol,您可以使用(但不一定需要)私有键枚举。如果是这样,请在您的类中声明它
public class DemoModel: NSObject, NSCoding {
private enum Keys: String {
case id, name, collectionName // one case for any property that shall be persisted plus one for the collectionName
}
var modelId: String
var modelName: String
(...)
}
您想要持久化的每个属性都需要一个键。请注意,一个属性必须唯一(主键)。您可以使用此属性来实现 identifier()。在大多数情况下,它将是一个 UUID。
您还可以在枚举中为 collectionName() 添加一个案例。
您必须实现 collectionName() 和 identifier(),它们返回用于识别的字符串。collectionName() 识别通用类型,identifier() 识别单个模型。
extension DemoModel: CanBePersistedProtocol {
// an identifier for a group. Must be unique amongst a Type.
public static func collectionName() -> String {
return Keys.collectionName.rawValue
}
// an identifier for a single model. Must be unique. It is common to use UUIDs here.
public func identifier() -> String {
return self.modelId
}
}
实现 NSCoding
public class DemoModel: NSObject, NSCoding {
(...)
var modelId: String
var modelName: String
(...)
// An initializer that the store will use to resurrect your model
public required init?(coder aDecoder: NSCoder) {
if let id = aDecoder.decodeObject(forKey: Keys.id.rawValue) as? String, let modelName = aDecoder.decodeObject(forKey: Keys.name.rawValue) as? String {
self.modelId = id
self.modelName = modelName
} else {
fatalError("could not decode object \(#file)")
}
}
// The method that the store will use to persist your model
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.modelId, forKey: Keys.id.rawValue)
aCoder.encode(self.modelName, forKey: Keys.name.rawValue)
}
}
实现 NSCoding 简单明了。但请注意,可选项不能被持久化,某些情况下需要转换。
这意味着您在解码时应该检查 nil,否则您的应用程序可能会崩溃。字符串可以作为 Object 解码,并需要转换为 String。如果您的类有需要持久化的非基本类型的成员,请参阅此处:https://stackoverflow.com/questions/43060636/how-do-i-save-persist-class-objects-that-include-other-classes-with-swift
创建商店
let store = NSCodingPersistenceStore(databaseFilename: "db" + UUID().uuidString, version: 0)
除非您想迁移或续订您的数据库,UUID应保持不变。
数据库更新/迁移的文档将随后提供。
持久化和获取单个条目
以这种方式持久化的模式
let demoModelToPersist = DemoModel(modelId: "38AFCFBE-9EC9-45A3-AAC8-B0164E5ACD5A", modelName: "the single model")
try! store.persist(demoModelToPersist)
可以很容易地以这种方式获取
let deSerializedModel = try! store.get("38AFCFBE-9EC9-45A3-AAC8-B0164E5ACD5A", type: DemoModel.self)
持久化和获取多个条目
以这种方式持久化的多个模式
var multipleItems = [
DemoModel(modelId: "38AFCFBE-9EC9-45A3-AAC8-B0164E5ACD5A", modelName: "first model"),
DemoModel(modelId: "C3263826-347E-4CAC-B142-0392A11BBE44", modelName: "second model"),
DemoModel(modelId: "D7CC435A-6975-4089-99E8-80A53B3877C3", modelName: "third model")
]
try! store.persist(multipleItems)
无法轻松获取。但有一个方法可以获取特定类型所有条目的所有项。
var multipleItems = [DemoModel]()
try! multipleItems = store.getAll(DemoModel.self)
let yourChoice = multipleItems.filter { /* your magic here */ }
使用视图来获取条目
将随后提供
检查是否存在
您可以通过以下方式检查项目是否已被持久化:
let id = UUID().uuidString
let itemFromSomewhere = DemoModel(modelId: id, modelName: "assume, this item is from an api ore somewhere else")
let itemExits = try! store.exists(itemFromSomewhere)
if itemExits {
// your reaction
} else {
// your reaction
}
您还可以通过标识符来检查存在性
let itemExists = try! store.exists("45F21990-7C4E-4506-882C-F6C5DBFB5C5B", type: DemoModel.self)
或者,如果您需要具有完成处理器的删除
try! store.exists("45F21990-7C4E-4506-882C-F6C5DBFB5C5B", type: DemoModel.self, completion: { (exists: Bool) in
if exists {
// your reaction here
} else {
// your reaction here
}
})
删除条目
可以像这样删除项目:
let itemToDelete = try! store.get("4FA9578E-D33F-4DBA-98E4-BEA340EEC992", type: DemoModel.self)
try! store.delete(itemToDelete)
如果您在删除项目后需要执行操作,您可以使用完成处理器。如果您想相应地更新您的用户界面,这很有用。
let itemToDelete = try! store.get("BD5B131F-54A1-4072-85BE-5176945A168E", type: DemoModel.self)
try! store.delete(itemToDelete, completion: {
updateUI() // your reaction here
})
作者
Jan Bartel, [email protected]
许可证
JBPersistenceStore 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。