Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 235
Manish Agrawal Avatar asked Jan 09 '12 20:01

Manish Agrawal


People also ask

How SQLite manage data in iOS apps?

Step 1 − Create a simple View based application. Step 2 − Select your project file, then select targets and then add libsqlite3. dylib library in choose frameworks. Step 3 − Create a new file by selecting File→ New → File...

Does SQLite work on iOS?

SQLite is available by default on iOS. In fact, if you've used Core Data before, you've already used SQLite.

How do you access SQLite using iOS mobile application?

Step 1 − Open Xcode -→ Single View Application -→ Let's name is DBSqlite. Step 2 − Let's develop our UI, Open Main. storyboard and add one text field and two buttons as shown below. Step 3 − Create @IBAction for both the buttons and @IBOutlet for text field and name them btnInsert, btnShowData and name respectively.

How can I see SQLite database in iOS?

Open DBBrowser and open database and in finder press CMD+SHIFT+G and paste your link there and press ENTER. You will be redirected to that location. 4. Select your SQLite file and it will be opened in DBBrowser.


1 Answers

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!

like image 119
antf Avatar answered Oct 15 '22 13:10

antf