2

this morning we had a big trouble with our iphone app. We had to even take it off the store.

The thing is that we made real small changes to our xcdatamodel. We thought that the update process is automatically taking care about exchanging it the right way until we found out something like CoreData migration exists.

We are using the UIManagedDocument to connect to the persistent store.

How is it possible to exchange this file with the new one? While we were developing we just uninstalled the whole app from the device and then installed it again and everything worked. How can we simulate this process in the app store with updates?

UPDATE

I try to set the migration option like this

_database = [[UIIManagedDocument alloc] init];
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption],

_database.persistentStoreOptions = options;

but the app is still crashing with

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'


MrBr
  • 1,884
  • 2
  • 24
  • 38

1 Answers1

2

The key to this is managing multiple versions of your xcdatamodel file. One version is the current one and is used to open data stores created according to older versions. When doing so, you use a migration mapping to convert from one model to the other. This is all explained here.

As to your questions: how you simulate the update process, actually you do not need to do anything special:

  1. install the older version;

  2. populate its data store with some data;

  3. install through Xcode the newer version on top of the older;

The newer version will have the newer data model; when installed on top of the older version, it will find the older data store and will have to try and migrate it. This is exactly what happens when doing an update through the App Store: the app binary is replaced, but its sandbox data is kept intact, so the new binary will find it already there.

Hope this helps.

EDIT:

Your code is shortened so I am not sure you have not yet, but you could try with:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

Also have a look at this post.

On another coin, automatic migration will not always work, it depends on the kind of changes you made to the model; when it does not work, the you need specify a custom migration model. This is a very informative post about the topic.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123