Reputation: 11
Currently I have an app shipped to App Store. I want to include the populated database to the next version(I will copy the .sqlite file to the main bundle), but some data will need to be loaded, depending on location. The next version's data model has significant changes. Furthermore, next versions would also have changes to data model. What do I do?
Do I need to care of migration if I ship the DB? If I check whether the DB file exists, and if it's not, copy it from the main bundle - it won't affect current users, but if I won't check - it would get replaced every time user launches an app. It's still unclear to me how to deal with next versions. I assume I need to keep the shipped DB named equally from version to version, but it will cause the data loss on update.
Upvotes: 0
Views: 66
Reputation: 960
This is a classically challenging problem. What we've done on iOS is store the migration version number of the database somewhere, potentially in NSUserDefaults, then step through a series of if
statements that check to see if the deployed version number is less than a desired version number. If the if
statement is triggered, then the migration corresponding to that version upgrade occurs, and then the version number itself is incremented.
double version = [self getDatabaseMigrationVersion];
if(version < 1.1)
{
//1.0 -> 1.1 SQL migration
version = 1.1;
[self setDatabaseMigrationVersion:version];
}
if(version < 1.2)
{
//1.1 -> 1.2 SQL migration
version = 1.2;
[self setDatabaseMigrationVersion:version];
}
//...
It's important to remember that app updates are not uniformly distributed among users, and so you absolutely cannot assume everyone has performed all the relevant migrations. You are stuck maintaining a sequence of incremental steps. For instance, even if you're at 1.4 after 1.3, 1.2, and 1.1, you likely still have users who are on 1.0, and so on, so you cannot abandon any of the intermediate steps.
Apparently there are some libraries to help manage SQLite migrations on iOS. I'm not linking to them because I haven't reviewed them. I doubt they are much more than simple wrappers around the process I mentioned above.
Upvotes: 0