5

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

Community
  • 1
  • 1
Smith
  • 379
  • 6
  • 18
  • What was "of no use"? Have you read the Apple guide? – Wain Sep 04 '13 at 17:10
  • Why are you reading about migration if you want to delete the old database? That's not a form of migration. You want to read about [`– removeItemAtPath:error:`](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/removeItemAtPath:error:) of `NSFileManager`. Or should I ask why you are talking about deletion if you want to migrate? I hope that there is not a single piece of user data in this database. – Matthias Bauch Sep 04 '13 at 17:34

2 Answers2

10

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!

Piyush
  • 188
  • 8
  • thanks for your reply after Mapping Model, do i need to do any code changes that saying that i have done with mapping model in + (NSPersistentStoreCoordinator *)persistentStoreCoordinator method? – Smith Sep 25 '13 at 08:39
  • Nothing at all, just make sure you choose the latest Model Version from the .xcdatamodel – Piyush Sep 25 '13 at 08:50
  • Hi @Piyush, I did what you said, and it worked perfectly, Thanks... do I have to create and keep a mapping model for every upgrade? – Balz Mar 03 '14 at 21:52
  • @Balz If you do any changes in the model, then it will incompatible with the earlier version. For changes in the model with respect to the changing attributes etc you can use [Lightweight Migration](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html). Check the link for more information. – Piyush Mar 04 '14 at 05:36
  • Fantastic. Thank you – JerseyDevel Apr 06 '21 at 22:23
  • I realize this is old, but it still helped me. Aside from this, is there any code (using Swift) that needs to be inserted telling the app to update the database if applicable? I do not see anything telling my PersistentStorage to migrate if needed.. – JerseyDevel Apr 07 '21 at 12:45
2

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.

karlsburg
  • 185
  • 1
  • 10