Last active
August 29, 2015 14:24
-
-
Save vknabel/f203cdd216d758ca3b38 to your computer and use it in GitHub Desktop.
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
import CoreData | |
/** | |
* This is a generic source for all CoreData based sources. Subclass to focus on essentials. | |
*/ | |
public class CoreDataSource { | |
let modelURL: NSURL | |
let storeURL: NSURL | |
public init(modelURL: NSURL, storeURL: NSURL) { | |
self.modelURL = modelURL | |
self.storeURL = storeURL | |
} | |
var _managedObjectContext: NSManagedObjectContext? = nil | |
public func saveContext() { | |
var error: NSError? = nil | |
let managedObjectContext = self.managedObjectContext | |
if managedObjectContext.hasChanges && !managedObjectContext.save(&error) { | |
// Replace this implementation with code to handle the error appropriately. | |
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
//println("Unresolved error \(error), \(error.userInfo)") | |
abort() | |
} | |
} | |
// MARK: Core Data stack | |
// 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. | |
public var managedObjectContext: NSManagedObjectContext { | |
if _managedObjectContext == nil { | |
let coordinator = self.persistentStoreCoordinator | |
_managedObjectContext = NSManagedObjectContext() | |
_managedObjectContext!.persistentStoreCoordinator = coordinator | |
} | |
return _managedObjectContext! | |
} | |
// Returns the managed object model for the application. | |
// If the model doesn't already exist, it is created from the application's model. | |
public var managedObjectModel: NSManagedObjectModel { | |
if _managedObjectModel == nil { | |
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL) | |
} | |
return _managedObjectModel! | |
} | |
var _managedObjectModel: NSManagedObjectModel? = nil | |
// 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. | |
public var persistentStoreCoordinator: NSPersistentStoreCoordinator { | |
if _persistentStoreCoordinator != nil { | |
var error: NSError? = nil | |
_persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) | |
if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil { | |
/* | |
Replace this implementation with code to handle the error appropriately. | |
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
Typical reasons for an error here include: | |
* The persistent store is not accessible; | |
* The schema for the persistent store is incompatible with current managed object model. | |
Check the error message to determine what the actual problem was. | |
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. | |
If you encounter schema incompatibility errors during development, you can reduce their frequency by: | |
* Simply deleting the existing store: | |
NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil) | |
* Performing automatic lightweight migration by passing the following dictionary as the options parameter: | |
[NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true} | |
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. | |
*/ | |
//println("Unresolved error \(error), \(error.userInfo)") | |
abort() | |
} | |
} | |
return _persistentStoreCoordinator! | |
} | |
var _persistentStoreCoordinator: NSPersistentStoreCoordinator? = nil | |
// MARK: Application's Documents directory | |
// Returns the URL to the application's Documents directory. | |
public class var documentsDirectory: NSURL { | |
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) | |
return urls[urls.endIndex-1] as! NSURL | |
} | |
public class var cachesDirectory: NSURL { | |
let urls = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask) | |
return urls[urls.endIndex-1] as! NSURL | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment