How to migrate core data by deleting old one on App Update
Asked Answered
P

2

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

Pekoe answered 4/9, 2013 at 16:20 Comment(2)
What was "of no use"? Have you read the Apple guide?Introduction
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: 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.Coverlet
K
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!

Kraul answered 24/9, 2013 at 9:30 Comment(6)
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?Pekoe
Nothing at all, just make sure you choose the latest Model Version from the .xcdatamodelKraul
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?Tambourin
@Tambourin 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. Check the link for more information.Kraul
Fantastic. Thank youControversy
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..Controversy
V
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.

Viveca answered 4/9, 2013 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.