5

I've found some other question but I didn't get any clear idea how to keep the data from old database while application update in ios.

Case 1: Can I keep the old database?

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

if case 1 is NO: Can I get the old database data in new database? Will the old database will be removed?

Case 2: If I give a different name to new data base (it'll be included in bundle)? If giving a new name keeps the old database can I delete the old database programatically?

What will be the best practice? Give a new name to database file for keeping the old one and then copy the old to to new database and delete the old database file? Just start using the old one?

Looking for help.. :)

3 Answers 3

3

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)

1
  • @Rashad: That'll depend on your need. In my opinion using old table will be a good Idea.
    – Midhun MP
    Commented Jan 30, 2014 at 6:00
0

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.

1
  • It's depend on the scenario where if you need to just update little data and structure is not changing a lot then keep the old one else import the data into new one
    – Retro
    Commented Jan 30, 2014 at 6:02
0

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.

1
  • I'll give it a try. My app is not already deployed but it'll have 3 version, I am planning to release the V1 quickly and for that I need to be clear about using database. Yesterday i tried to use sqlite. but I could able to store data by code. Is there any access issue?
    – Rashad
    Commented Jan 31, 2014 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.