CoreDataKit is no longer being updated with new features. It is used in several projects, and probably will be updated with new Swift versions. But it is no longer actively developed.
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.
CocoaPods is the advised way to include CoreDataKit into your project. A basic Podfile including CoreDataKit would look like this:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'CoreDataKit'
- Swift 4: Use 0.13 and up
- Swift 3: Use 0.12
- Swift 2.3: Use 0.11
- Swift 2.2: Use 0.10
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) {
CDK.sharedStack = CoreDataStack(persistentStoreCoordinator: persistentStoreCoordinator)
}
For CoreDataKit to be able to use your NSManagedObject subclass, such as Car in the example below, CDK needs to have the subclass implement the NamedManagedObject protocol in order to be able to initialize your class:
class Car: NSManagedObject, NamedManagedObject {
static var entityName = "Car" // corresponding to your Entity name in your xcdatamodeld
@NSManaged var color: String
@NSManaged var model: String
}
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:
CDK.performOnBackgroundContext(block: { context in
do {
let car = try context.create(Car.self)
car.color = "Hammerhead Silver"
car.model = "Aston Martin DB9"
return .saveToPersistentStore
}
catch {
return .doNothing
}
}, completionHandler: { result in
do {
try result()
print("Car saved, time to update the interface!")
}
catch {
print("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 = CDK.performOnBackgroundContextPromise { context in
do {
let car = try context.create(Car.self)
car.color = "Hammerhead Silver"
car.model = "Aston Martin DB9"
return .saveToPersistentStore
}
catch {
return .doNothing
}
}
createPromise
.then { _ in
print("Car saved, time to update the interface!")
}
.trap { error in
print("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.