CoreDataKit takes care of the hard and verbose parts of CoreData. It manages child contexts for you and helps to easily fetch, create and delete objects.
CoreData: object graph management solution, including persistence. Kit: set of equipment needed for a specific purpose.
Due to the current lack of proper infrastructure for Swift dependency management, using CoreDataKit in your project requires the following steps:
- Add CoreDataKit as a submodule by opening the Terminal,
cd
-ing into your top-level project directory, and entering the commandgit submodule add https://github.com/mac-cain13/CoreDataKit.git
- Open the
CoreDataKit
folder, and dragCoreDataKit.xcodeproj
into the file navigator of your app project. - In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
- Ensure that the deployment target of CoreDataKit.framework matches that of the application target.
- In the tab bar at the top of that window, open the "Build Phases" panel.
- Expand the "Target Dependencies" group, and add
CoreDataKit.framework
. - Click on the
+
button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and addCoreDataKit.framework
.
The most basic and most used variant to setup a stack backed by an automigrating SQLite store is this:
// Initialize CoreData stack
if let persistentStoreCoordinator = NSPersistentStoreCoordinator(automigrating: true) {
CoreDataKit.sharedStack = CoreDataStack(persistentStoreCoordinator: persistentStoreCoordinator)
}
From here you are able to use the shared stack. For example to create and save an entity, this example performs a block an a background context, saves it to the persistent store and executes a completion handler:
CoreDataKit.performBlockOnBackgroundContext({ context in
if let car = context.create(Car.self).value() {
car.color = "Hammerhead Silver"
car.model = "Aston Martin DB9"
}
return .SaveToPersistentStore
}, completionHandler: { result, _ in
switch result {
case .Success:
println("Car saved, time to update the interface!")
case let .Failure(error):
println("Saving Harvey Specters car failed with error: \(error)")
}
})
If you prefer using promises, instead of the callback style of this library, you can use the Promissum library with CoreDataKit. Using the CoreDataKit+Promise extension, the example from above can be rewritten as such:
let createPromise = CoreDataKit.performBlockOnBackgroundContextPromise { context in
if let car = context.create(Car.self).value() {
car.color = "Hammerhead Silver"
car.model = "Aston Martin DB9"
}
return .SaveToPersistentStore
}
createPromise.then { _ in
println("Car saved, time to update the interface!")
}.catch { error in
println("Saving Harvey Specters car failed with error: \(error)")
}
We'll love contributions, please report bugs in the issue tracker, create pull request (please branch of develop
) and suggest new great features (also in the issue tracker).
CoreDataKit is written by Mathijs Kadijk and available under the MIT license, so feel free to use it in commercial and non-commercial projects. CoreDataKit is inspired on MagicalRecord.