Manish Agrawal Manish Agrawal - 1 year ago 63
iOS Question

sqlite database update when app version changes on Appstore in iPhone

I have an app with version 1.0 on app store which uses sqlite database for reading the data.
Now I want to update my version to 1.1 with update in database file.
While using developer certificate when I install app on device it did not update the database as the database file already exist in documents folder so i have to manually delete the app and install it again.
My question is, when any user update the app, will the database also get updated according the current version.
Any suggestions are welcome.
Thanks

Answer Source

I am sure there are many ways to do this (and many ways better then mine as well), but the way that I handle such problems is as follows:

First I define a constant in the first .h file of the app (the one that will load first) to indicate First Time load and set it to 0:

#define FirstTime 0

Now you have to know that I have the intention to save the value of this constant in the Documents folder for future references, therefore I use a Shared Data Instance. In the viewDidLoad I do the following test:

//if first time run of this version
if( [MyDataModel sharedInstance].count < (FirstTime + 1) )
{
    //do what you need to do as the first time load for this version

    [MyDataModel sharedInstance].count++
    //save the count value to disk so on next run you are not first time
    //this means count = 1
}

Now the trick is on your new app version (say 1.1). I change the FirstTime to 2:

#define FirstTime 2

Since the saved First Time value on disc is 1 this means you will be caught by the if statement above, therefore inside it you can do anything you want like delete the old tables and recreate them again with the new formation.

Again not that brilliant, but solves the case!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download