Swift CoreDataManager for multithreading environments. This is running under XCode 6 beta - please notice this.
This Swift CoreData Manager can handle multi-threading. You can access to CoreData entities from different threads or GCD dispatches closures without problems.
As simple as copying the file CoreDataManager.swift
into your project.
This manager uses the singleton pattern in order to be able of handle different NSManagedObjectContexts per thread automatically.
executeFetchRequest(request:)
Fetches results synchronously
var request: NSFetchRequest = NSFetchRequest(entityName: "MonkeyEntity")
var myMonkeys:NSArray? = CoreDataManager.shared.executeFetchRequest(request)
executeFetchRequest(request:, completionHandler:)
Fetches with a completionHandler that returns an array with results asynchronously.
var myMonkeys:NSArray?
var request: NSFetchRequest = NSFetchRequest(entityName: "MonkeyEntity")
CoreDataManager.shared.executeFetchRequest(request) { results in
myMonkeys = results!
}
save()
Saves the context, it doesn't matters the thread.
CoreDataManager.shared.save()
deleteEntity(object:)
Deletes the NSManagedObject sent
CoreDataManager.shared.deleteEntity(item as MonkeyEntity)
Tests are provided to check the performance under a stress case.