CoreDataDandy 0.6.1

CoreDataDandy 0.6.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Noah Blake.



header

Introduction

Core Data Dandy is a feature-light wrapper around Core Data that simplifies common database operations.

Feature summary

  • Initializes and maintains a Core Data stack.
  • Provides convenience methods for saves, inserts, fetches, and deletes.
  • Maps json into NSManagedObjects via a lightweight API.
  • Deserializes NSManagedObjects into json

Usage

All standard usage of Core Data Dandy should flow through CoreDataDandy’s sharedDandy. More advanced users, however, may find its various components useful in isolation.

Bootstrapping

CoreDataDandy.wake("ModelName")

Saving and deleting

Save with or without a closure.

Dandy.save()
Dandy.save { error in
    // Respond to save completion.
}

Delete with or without a closure.

Dandy.delete(object)
Dandy.delete(object) {
    // Respond to deletion completion.
}

Destroy the contents of the database. Called, for example, to recover from a failure to perform a migration.

Dandy.tearDown()

Fetching

Fetch all objects of a given type.

Dandy.fetch(Gossip.self)

Fetch an object corresponding to an entity and primaryKey value.

Dandy.fetchUnique(Hat.self, identifiedBy: "bowler")

Fetch an array of objects filtered by a predicate.

Dandy.fetch(Gossip.self, filterBy: NSPredicate(format: "topic == %@", "John Keats"))

Insertions and updates

Insert object of a given type.

Dandy.insert(Gossip.self)

Insert or fetch a unique a object from a primary key.

Dandy.insertUnique(Slander.self, identifiedBy: "WILDE")

Upsert a unique object, or insert and update a non-unique object.

Dandy.upsert(Gossip.self, from: json)

Upsert an array of unique objects, or insert and update non-unique objects.

Dandy.batchUpsert(Gossip.self, from: json)

Mapping finalization

Objects requiring custom mapping finalization should adopt the MappingFinalizer protocol. The protocol has a single function, finalizeMapping(_:).

extension Conclusion: MappingFinalizer {
    func finalizeMapping(of json: [String : AnyObject]) {
        if var content = content {
            content += "_FINALIZED"
            self.content = content
        }
    }
}

Serialization

Serialize a single object.

Serializer.serialize(gossip)

Serialize an array of objects.

Serializer.serialize([byron, wilde, andre3000])

Serialize an object and its relationships.

Serializer.serialize(gossip, including: ["purveyor"])

Serialize an object and its nested relationships.

Serializer.serialize(gossip, including: ["purveyor.hats.material, purveyor.predecessor"])

xcdatamodel decorations

CoreDataDandy supports four xcdatamodel attributes. All decorations are declared and documented in DandyConstants.

@primaryKey

Add this decoration to the entity’s userInfo to specify which property on the entity functions as its primaryKey.

@mapping

Add this decoration to a property to specify an alternate mapping for this property. For instance, if a property is named “abbreviatedState,” but the json value for this property is found at the key “state,” add @mapping : state to the abbreviatedState’s userInfo.

@false

Use this decoration in conjunction with the @mapping keyword to disable mapping to the property. For instance, if your entity has an attribute named “secret” that you’d prefer to map yourself, add @mapping : @false to secret’s userInfo.

@singleton

Add this decoration to an entity’s userInfo if there should never be more than one instance of this entity in your database. This decoration may be useful for objects like Tokens and CurrentUsers, though it’s primarily included to suggest the kind of decorations that may be added in the future.

Warnings

To receive console warnings in Swift projects, add the entry -D DEBUG in your project’s build settings under Swift Compiler - Custom Flags.