Duvel 1.0.0

Duvel 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2016
SPMSupports SPM

Maintained by Jelle Vandebeeck, Leroy Jenkins.



Duvel 1.0.0

Duvel makes using Core Data more friendlier than ever with Swift.

TOC

Installation

Duvel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Duvel', '~> 1.0'

Features

Initialize

Initialize the usage of Duvel by using a Duvel instance. When initializing the instance you will create the persistent store.

// Create the default store.
let duvel = try! Duvel()

// Create an in memory store.
let duvel = try! Duvel(storeType: NSInMemoryStoreType)

// Create a store with a custom managed object model.
let managedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil)
let duvel = try! Duvel(managedObjectModel: managedObjectModel)

Is you want to instantiate Duvel as a sharedInstance that you want to use throughout your application without passing the instance between the different classes. Than extend Duvel like this.

extension Duvel {
    static var sharedInstance: Duvel? = {
        // Add more configuration to the instance as shown in the above code.
        return try? Duvel()
    }()
}

// This will encapsulate `Duvel` in an singleton instance.
Duvel.sharedInstance

Context

There are three ways to access some contexts.

// Get the main `NSManagedObjectContext`.
Duvel.sharedInstance.mainContext

// Get the background `NSManagedObjectContext`.
Duvel.sharedInstance.backgroundContext

// Get the `NSManagedObjectContext` for the current thread.
Duvel.sharedInstance.currentContext

Changes made on the backgroundContext are automatically merged to the mainContext when succeeded.

Creation

You can create a NSManagedObject in a context. The NSManagedObject you want to create has to conform to the ManagedObjectType protocol. In this case SomeManagedObject conforms to ManagedObjectType.

let context: NSManagedObjectContext = ...
let object = SomeManagedObject.create(in: context)

We can also immediately set the properties during the creation process.

let context: NSManagedObjectContext = ...
let object = SomeManagedObject.create(in: context) { innerObject in
  innerObject.name = "Leroy"
}

BE AWARE! Nothing is persisted to the store until you save.

Deletion

There is a possibility to delete all the NSManagedObject’s depending on the given NSPredicate. When no predicate is given we delete all the objects for the given type.

let context: NSManagedObjectContext = ...

// Delete all the objects.
SomeManagedObject.deleteAll(in: context)

// Delete the objects that match a predicate.
let predicate: NSPredicate = ...
SomeManagedObject.deleteAll(in: context, with: predicate)

// Delete one the object.
let object: SomeManagedObject = ...
object.delete(inContext: context)

If you just want to delete a single object, you can use the one defined in Core Data.

let context: NSManagedObjectContext = ...
SomeManagedObject.deleteObject(in: context)

BE AWARE! Nothing is persisted to the store until you save.

Fetching

Fetch the first NSManagedObject depending on the value of the attribute.

When no object was found for this value, you can create a new one with the attribute set to the given value. This will be done when the createIfNeeded parameter is set to true.

let context: NSManagedObjectContext = ...
let object = SomeManagedObject.first(in: context, with: "name", value: "Leroy")
let object = SomeManagedObject.first(in: context, with: "name", value: "Leroy", createIfNeeded: true)

You can also look for the first NSManagedObject found for the given predicate. You can also indicate the sort order of the fetched results so that you can eventually fetch the last object.

let predicate: NSPredicate = ...
let descriptors: [NSSortDescriptor] = ...
let context: NSManagedObjectContext = ...
let object = SomeManagedObject.first(in: context, with: predicate, sort: descriptors)

Fetch all the objects for a certain type. You can specify a filter by giving an NSPredicate and a sort order by providing a list of NSSortDescriptor’s.

let predicate: NSPredicate = ...
let descriptors: [NSSortDescriptor] = ...
let context: NSManagedObjectContext = ...
let objects = SomeManagedObject.all(in: context, with: predicate, sort: descriptors)

Count the number of object. You can specify a filter by giving an NSPredicate.

let predicate: NSPredicate = ...
let context: NSManagedObjectContext = ...
let count = SomeManagedObject.count(in: context, with: predicate)

Saving

Persist the changes to the store by using the perform function provided on the context.

This function will apply the changes to a child context that is created from the initial context. And when these changes are successfully applies they will be merged into the initial context.

context.perform(changes: { localContext in
  // Modify objects inside this closure. At the end of the closure the changes will be
  // automatically persisted to the store.
  // Make sure you do the changes on the given `localContext`.
}, completion: {
  // This completion closure will be called when `Duvel` finished writing to the store.
})

Sometimes you’ll want update an existing NSManagedObject. If you want to do so and use the perform function, than you’ll probably need to convert to object to an object from the localContext.

let existingObject: SomeManagedObject = ...
context.perform(changes: { localContext in
  let localObject = existingObject.to(context: localContext)
  localObject.name = "Leroy"
}, completion: {
  ...
})

Bucket List

Here is an overview what is on our todo list.

  • [ ] Type safety for attribute creation.
  • [ ] Add notifications.

Author

Jelle Vandebeeck, [email protected]

License

Duvel is available under the MIT license. See the LICENSE file for more info.