iOS - Keep the old sqlite database while updating to new version iOS - Keep the old sqlite database while updating to new version sqlite sqlite

iOS - Keep the old sqlite database while updating to new version


Case 1: Can I keep the old database?

Yes, updating your application won't delete the database file stored in the documents directory. (But if it is in your bundle, it'll be removed)

if Case 1 is YES: Can I insert new column or doing any changes in the old database and will it be safe?

That'll depend on your implementation. You can use ALTER TABLE query for adding or removing column. You need to handle the new changes in your code, else it can cause problems.Adding a column won't cause any issues in normal scenarios (Depends on your insert query statements and new field constraints)


It's fairly Case 1 as assuming you have copy your .sqlite file to document folder and when you update the application it will look for the database the can update its table and update its database without loosing anything.


If I understand your question (you're looking to update a model that's already deployed via the App Store), yes you can perform upgrades to an existing model using the .xcdatamodeld format in Xcode. The Core Data Versioning doc from Apple covers this topic comprehensively.

This can be a fiddly process, if you have precious user data stored in your model, you'll want to test this exhaustively before pushing out your updates.

To add a new version to your model;

  • Select your xcdatamodel file (e.g. model.xcdatamodel)
  • Click Editor > Add Model Version
  • Name the new model version (e.g. Model 2), based on Model 1
  • Open the Utilities pane
  • Select 'Model 2' for the Current Model Version

Then add this method to your controller class implementation file, to help make small changes to the data model (changing field types, adding columns etc).

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{    if (_persistentStoreCoordinator != nil) {        return _persistentStoreCoordinator;    }    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Model.sqlite"];    NSError *error = nil;    // for performing minor changes to the coredata database    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],                         NSMigratePersistentStoresAutomaticallyOption,                         [NSNumber numberWithBool:YES],                         NSInferMappingModelAutomaticallyOption, nil];    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);        abort();    }    return _persistentStoreCoordinator;}

Also, worth noting: Apple recommends you only store user relevant files in the Documents folder. SQLite database files and similar should be stored in /Library/Application Support.