I have a project that is using core data , i need add more attributes(Columns) to existing entity (Column) , if i manually add attribute to data model app crash and it is due t
So my problem was I had no idea where this persistent store coordinator code goes. It turns out it is automatically created in your AppDelegate
implementation when you check "Use Core Data" when creating the project.
So, from the second link here, all you need to do for a light-weight migration (adding new attributes and such) is the following:
Change your AppDelegate persistent store coordinator code as follows.
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(".sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
let options = [
NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error) == nil {
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this 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.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
return coordinator
}()
So you only add migration options to the addPersistentStoreWithType
call.