Slide 17
Slide 17 text
One Way To Set Up The Stack
func initializeCoreDataStack() {
guard let modelURL = NSBundle.mainBundle().URLForResource("PPRecipes",
withExtension: "momd") else {
fatalError("Failed to locate DataModel.momd in app bundle")
}
guard let mom = NSManagedObjectModel(contentsOfURL: modelURL) else {
fatalError("Failed to initialize MOM")
}
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
let type = NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType
mainContext = NSManagedObjectContext(concurrencyType: type)
mainContext?.persistentStoreCoordinator = psc
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) {
let fileManager = NSFileManager.defaultManager()
guard let documentsURL = fileManager.URLsForDirectory(.DocumentDirectory,
inDomains: .UserDomainMask).first else {
fatalError("Failed to resolve documents directory")
}
let storeURL = documentsURL.URLByAppendingPathComponent("PPRecipes.sqlite")
do {
try psc.addPersistentStoreWithType(NSSQLiteStoreType,
configuration: nil, URL: storeURL, options: nil)
} catch {
fatalError("Failed to initialize PSC: \(error)")
}
self.populateTypeEntities()
self.persistenceInitialized = true
dispatch_sync(dispatch_get_main_queue()) {
self.initializationComplete?()
}
}
}