I developed an app, iCollege, and now I want to make the app much better.
While the testing I wanted to restore the data from a backup. At launching iCollege the app crashes
While theoretically, you can migrate your data manually, it's probably not worth the effort. You want to use automatic lightweight migration
.
To perform automatic lightweight migration, you need to have two data models set up, then in code, you tell Core Data to perform the migration. This means that you should not modify your data model until you finish reading this. If you have, (automatically or manually) restore your old model. You'll need it to do the migration. Here's how automatic lightweight migration works:
First, you need to add a model version to your data model. Select the existing model and then add a version from the Editor
menu:
You will be prompted to name your data model and choose which existing model to base it on.
Now, go ahead and make your changes to the new model. When you are done, you need to tell Core Data to use this new model as the current version. We're not up to the code yet, so this part is easy. (The code is easy too.) On the right hand side panel, choose the active model, as shown here:
Make sure that your model is selected in the left hand navigator, or else you might not see the options on the right. You should end up with something like this:
(I'm actually using version 2 here instead of version 3, but the idea is the same.)
Now, you need to make a quick change to your code so that Core Data knows to do the migration for you.
Inside of your persistentStoreCoordinator
method in your app delegate, change this line:
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){
to the following (adding in the line preceding the if
statement):
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){
You've essentially passed Core Data a dictionary of options which tells it to migrate the data store for you. (Take a close look at the preceding code, it will make sense after a couple of read throughs.)
Edit: You probably can do what you want. If I understand you correctly, you should create a new model version, perform lightweight migration, and them manually make the changes that you want. My above answer still stands, except that you will want to make some manual changes afterward.