DKDBManager 是围绕 Magical Record 的简单但非常有用的 CRUD 管理器。
它实现了数据库逻辑,帮助开发者管理实体,并让他专注于不仅仅是数据管理的事物。
主要概念是将表示实体的 JSON 字典导入到数据库中。
在一些模型类配置之后,只需要一个函数来 创建、读取、更新 或 删除 实体!
完整文档可在 CocoaDocs 提供。
此外,Wiki 包含有关库不同逻辑的所有信息和细节。
使用 DKDBManager
和 MagicalRecord
,您可以轻松创建、更新或删除实体。
要这样做您需要位于一个 savingContext 并使用适当的 DKDBManager 函数。
DKDBManager.saveWithBlock { (savingContext: NSManagedObjectContext) in
// Perform saving code here, against the `savingContext` instance.
// Everything done in this block will occur on a background thread.
let plane = Plane.crudEntityInContext(savingContext)
plane?.origin = "London"
plane?.destination = "Paris"
}
在此执行块结束时,所有更改都将保存(或 合并 )到默认上下文。
此后,新的 Plane
实体将在主线程的默认上下文中可用。
或者您可以使用 JSON 结构 CRUD 一个实体
let planeJSON = ["origin":"Paris", "destination":"London"]
Plane.crudEntityWithDictionary(planeJSON, inContext: savingContext, completion: { (entity: AnyObject?, state: DKDBManagedObjectState) in
// The CRUDed plane is referenced in the `entity`.
// Its actual state is described as follow:
switch state {
case .Create: // The entity has been created, it's all fresh new.
case .Update: // The entity has been updated, its attributes changed.
case .Save: // The entity has been saved, nothing happened.
case .Delete: // The entity has been removed.
}
})
state
变量描述了实体发生了什么。
建议实现其他功能,比如用一个给定的字典更新当前实体的功能。
没有这个函数,实体的属性将永远不会设置或更新。
给定的 dictionary
对象是用于启动 CRUD 过程 的同一个对象。
override func updateWithDictionary(dictionary: [NSObject : AnyObject]?, inContext savingContext: NSManagedObjectContext) {
super.updateWithDictionary(dictionary, inContext: savingContext)
// Update attributes
self.origin = GET_STRING(dictionary, "origin")
self.destination = GET_STRING(dictionary, "destination")
}
更多内容请参阅 Wiki!
查看示例项目
$> pod try DKDBManager
kevindelord, [email protected]