Search code examples
ioscore-datacore-data-migration

How can I force replacement of a CoreData store with an App update?


I have an App active in the AppStore which comes with a populated data store. Now I want to push a new version with a slightly updated schema and a new set of data. The old data doesn't matter. If I simply push a new version of the app, I believe existing users will encounter an error with an incompatible datastore... how can I avoid this? I don't need to migrate existing data, it's all in the new app.

EDIT:

FYI, here is the URL where I'm loading the persistent store in my object manager:

NSURL *storeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MyObjects" ofType:@"storedata"]];

Solution

  • EDIT:

    If you store the persistent store in the main bundle (hence it is read-only), you should not worry, because the new persistent store (also included in your app bundle, I suppose) will just plainly overwrite the old one.

    I think that deleting the existing store file and then creating a new one should do it.

    If _storeURL is the path to your store file, you can delete like this:

    NSError *error = nil;
    if ([fileManager fileExistsAtPath:_storeURL.path])
      [fileManager removeItemAtURL:_storeURL error:&error];
    

    Of course, this should be done only on the very first launch of your latest app version. This could be achieved by storing a key in NSUserDefaults after deleting/recreating the store and having the program do the delete/create only when on launch the key is not there.