Question

Hi I'm going to update my iOS app in appstore and this update contains database change so now how to migrate my existing core data by deleting old database of existing version on App update? I have referred Core Data Migration tutorial

Core Data Migration Post

Unfortunately of no use. Any help is appreciated in advance

Was it helpful?

Solution

Smith,
I presume you have done some schema changes, in the xcdatamodel
Always, Add a new Model Version (Select name.xcdatamodeld then Editor->Add model Version) before making any changes, if you have an app already submitted to App Store which is using the earlier model version.
Then,
Add a new file from Core Data Tab, as Mapping Model
Select, Source Model (Model Version which the submitted App is using)
Destination Model (Model Version in which you have done the Changes)

And you are done!

OTHER TIPS

Is it possible you haven't created a new version of the DB model before you applied the auto migration?

  1. Select [dbname].xcdatamodeld from project explorer.
  2. Select Editor->Add model Version.
  3. Select base it on your current model.
  4. Make sure you have the automigrate option on like so:

    -(NSPersistentStoreCoordinator *)storeCoordinator {
    
    if (storeCoordinator_ != nil) {
        return storeCoordinator_;
    }
    
    NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"[dbname].sqlite"]];
    
    NSDictionary* storeOptions = @{NSMigratePersistentStoresAutomaticallyOption : [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption : [NSNumber numberWithBool:YES]}; 
    
    NSError *error = nil;
    storeCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self objectModel]];
    
    if (![storeCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]) {
    
        // handle error here and remove abort
    
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return storeCoordinator_;
    }
    
  5. and away you go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top