Deleting the app is sometimes not the case! Suggest, your app has already been published! You can’t just add new entity to the data base and go ahead – you need to perform migration!
For those who doesn’t want to dig into documentation and is searching for a quick fix:
- Open your .xcdatamodeld file
- click on Editor
- select Add model version…
- Add a new version of your model (the new group of datamodels added)
- select the main file, open file inspector (right-hand panel)
- and under
Versioned core data model
select your new version of data model for current data model - THAT’S NOT ALL ) You should perform so called “light migration”.
- Go to your
AppDelegate
and find where thepersistentStoreCoordinator
is being created - Find this line
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
- Replace
nil
options with@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
(actually provided in the commented code in that method) - Here you go, have fun!
P.S. This only applies for lightweight migration. For your migration to qualify as a lightweight migration, your changes must be confined
to this narrow band:
- Add or remove a property (attribute or relationship).
- Make a nonoptional property optional.
- Make an optional attribute nonoptional, as long as you provide a default value.
- Add or remove an entity.
- Rename a property.
- Rename an entity.
For Swift 4
coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true])