Latest 1.0.0
Homepage https://github.com/icapps/ios-duvel
License MIT
Platforms ios 8.0, tvos 9.0, osx 10.10
Authors

Duvel

CI Status
License
Platform
Version
Language Swift 2.2

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 with a static shared function 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 shared: 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.shared

Context

There are three ways to access some contexts.

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

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

// Get the `NSManagedObjectContext` for the current thread.
Duvel.shared.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.

Latest podspec

{
    "name": "Duvel",
    "version": "1.0.0",
    "summary": "Use Core Data in a more friendly way with Swift.",
    "description": "Duvel contains a set of functions and utlities that make it easier for you to use Core Data in Swift.nnThis pod will make it easier to:n- Create a contextn- Insert, update and delete objects.n- Perform fetch requests.",
    "homepage": "https://github.com/icapps/ios-duvel",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Jelle Vandebeeck": "[email protected]"
    },
    "source": {
        "git": "https://github.com/icapps/ios-duvel.git",
        "tag": "1.0.0"
    },
    "social_media_url": "https://twitter.com/icapps",
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0",
        "osx": "10.10"
    },
    "source_files": "Sources/**/*"
}

Pin It on Pinterest

Share This