CoreDataSwagger
Extensions to CoreData providing a unified stack with configuration options and a simpler interface to common operations.
Unified Stack
Use a simpler interface to the various components of a CoreData implementation instead of fooling around with separate pieces.
Want a managed object model that is merged from all of the models in your main bundle? And a persistent store coordinator with a single in-memory store using that model? And a managed object context using private queue concurrency against that coordinator? Boom!
var stack = CoreDataStack()
Want a managed object model that uses specific models and store metadata? And you've got two different stores and need main queue concurrency? Only a few lines of Swagger code needed.
let modelSource = CoreDataModelSource(models: [modelOne, modelTwo], metadata: metadata)
let inMemoryStore = CoreDataStoreParameters()
let sqliteStore = CoreDataStoreParameters.SQLite(URL: storeURL, configuration: "Custom", options: nil)
let configuration = CoreDataStackConfiguration(concurrency: .MainQueueConcurrencyType, modelSource: modelSource, storeParameters: [inMemoryStore, sqliteStore])
var stack = CoreDataStack(configuration: configuration)
Simpler Fetch Results
You no longer need to provide error pointers to get fetch errors (currently not working - bug filed with Apple). All you need to do is use or ignore the error value in the tuple returned from a fetch. The return values are mutually exclusive, so you will get EITHER a result set OR an error, but never both or neither.
func fetch(request: NSFetchRequest) -> CoreDataObjectFetchResults
func fetchIDs(request: NSFetchRequest) -> CoreDataObjectIDFetchResults
func fetchDictionaries(request: NSFetchRequest) -> CoreDataDictionaryFetchResults
func count(request: NSFetchRequest) -> CoreDataCountFetchResults
func fetch(entityName: String) -> CoreDataObjectFetchResults
func fetch(entityDescription: NSEntityDescription) -> CoreDataObjectFetchResults
Where the following type aliases are used:
typealias CoreDataObjectFetchResults = ([NSManagedObject]?, NSError?)
typealias CoreDataObjectIDFetchResults = ([NSManagedObjectID]?, NSError?)
typealias CoreDataDictionaryFetchResults = ([AnyObject]?, NSError?)
typealias CoreDataCountFetchResults = (UInt?, NSError?)
Simpler Save and Perform Block And Wait
Saving changes to the managed object context is as easy as always, but now you don't have to provide an error pointer. Additionally, you can provide a closure to the save() method that will be executed before the save in a synchronous block via performBlockAndWait(). This save() method returns both a success flag and an optional error:
func save(closure: CoreDataPerformClosure? = nil) -> (Bool, NSError?)
(let success, let error) = stack.save()
(let success, let error) = stack.save() {
managedObject.property = "new value"
}
Entity and Property Retrieval
Find entities and their properties from the stack's more strongly-typed interface:
var entities: [NSEntityDescription]
var entitiesByName: [String:NSEntityDescription]
func entity(named name: String) -> NSEntityDescription?
func propertiesForEntity(named entityName: String) -> [NSPropertyDescription]?
func propertiesByNameForEntity(named entityName: String) -> [String:NSPropertyDescription]?
Or find the entity and properties associated with your custom class in the stack's managed object model:
class func entity(inStack stack: CoreDataStack) -> NSEntityDescription?
class func properties(inStack stack: CoreDataStack) -> [NSPropertyDescription]?
class func propertiesByName(inStack stack: CoreDataStack) -> [String:NSPropertyDescription]?
Object Deletion
Deleting objects is very easy with the stack's flexible interface:
func delete(objects: NSManagedObject...)
func delete(objects: [NSManagedObject])
func delete(identifiers: NSManagedObjectID...)
func delete(identifiers: [NSManagedObjectID])
func delete(fetchRequest: NSFetchRequest)
Fetched Results Controller Subscripting
Shortcuts to objectAtIndexPath
:
subscript(item: Int) -> NSManagedObject
(for single-section result sets)subscript(indexPath: NSIndexPath) -> NSManagedObject