Created
June 24, 2014 01:44
-
-
Save beccadax/d7c2f58cabd47d3eb30c to your computer and use it in GitHub Desktop.
Swift Core Data stack built with lazy properties. Warning: untested.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns the managed object context for the application. | |
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. | |
@lazy var managedObjectContext: NSManagedObjectContext = { | |
let context = (NSManagedObjectContext() as NSManagedObjectContext?)! | |
context.persistentStoreCoordinator = self.persistentStoreCoordinator | |
return context | |
}() | |
// Returns the managed object model for the application. | |
// If the model doesn't already exist, it is created from the application's model. | |
@lazy var managedObjectModel: NSManagedObjectModel = { | |
let modelURL = NSBundle.mainBundle().URLForResource("CoreData", withExtension: "momd") | |
return (NSManagedObjectModel(contentsOfURL: modelURL) as NSManagedObjectModel?)! | |
}() | |
// Returns the persistent store coordinator for the application. | |
// If the coordinator doesn't already exist, it is created and the application's store added to it. | |
@lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { | |
let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreData.sqlite") | |
var error: NSError? = nil | |
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) | |
if !coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) { | |
println("Unresolved error \(error), \(error?.userInfo)") | |
abort() | |
} | |
return coordinator | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment