1
votes

I have got the problem with the core data lightweight migration. Application with the first version of model was submitted to the AppStore. Then there was data model with version 2 (wich was not submitted). And finally after some changes I've added version 3 of the model and submit it to the AppStore.

Everything worked well on test device as migrations were done step by step (v1 - v2 - v3).

But it doesn't work for the users who updated the application from the AppStore such as it is trying to migrate from the version 1 and up to 3 (skipping version 2).

How can I solve this problem? Thanks.

2
Do you use a system control version like subversion or git?Ricardo

2 Answers

0
votes

It's a brute force solution, but deleting the store is an option to solve your issue:

// Setup CoreData with MagicalRecord && remove CoreData store
// The flag is used to perform the delete just once
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"CORE_DATA_FLAG01"]) {
    NSURL *storeURL =[NSPersistentStore MR_urlForStoreName:@"YourStore.sqlite"];
    LOG(@"%@", storeURL);
    NSFileManager *fm = [[NSFileManager alloc] init];
    NSError *error = nil;
    [fm removeItemAtURL:storeURL error:&error];
    if (error) {
        LOG(@"error %@", error);
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"CORE_DATA_FLAG01"];
    }
}

I'm using MagicalRecord to interact with CoreData. In this case I use the function to get the store.

0
votes

With lightweight migration, if V1->V2 works and V2->V3 works then there shouldn't be a problem going directly from V1->V3 - unless you messed something up. :)

Fire up the V1 version and then switch to V3 and step through and debug the issue.

NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption: @YES,
                          NSInferMappingModelAutomaticallyOption: @YES
                         };
NSError *error;
NSPersistentStore *persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error];
if (!persistentStore) {
    NSLog(@"migration failed: %@", error);
}