DKDBManager is a simple, yet very useful, CRUD manager around Magical Record.
It implements a logic around the database that helps the developer to manage his entities and let him focus on other things than only data management.
The main concept is to import JSON dictionaries representing entities into the database.
After some configuration on your model classes, only one single function is needed to create, read, update or delete your entities!
The complete documentation is available on CocoaDocs.
More over, the Wiki contains all kind of information and details about the different logics of the library.
With the DKDBManager
and MagicalRecord
you can easily create, update or delete entities.
To do so you need to be in a savingContext and use the appropriate DKDBManager functions:
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"
}
At the end of this execution block, all changes will be saved ( or merged ) into the default context.
After that, a new Plane
entity will be available on the main thread within the default context.
Or you could CRUD an entity by using a JSON structure:
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.
}
})
The state
variable describes what happened to the entity.
The implementation of other functions is also recommended such as the one to update the current entity with a given dictionary.
Without this function the attributes of the entity will never be set nor updated.
The given dictionary
object is the same one that has been used to start the CRUD process.
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")
}
Read more in the Wiki!
Checkout the example project:
$> pod try DKDBManager
kevindelord, [email protected]