Android Pushing Updates on Play Store
Asked Answered
B

5

10

I have an app that depends on SQLite for data which is populated by xmls shipped with the app in the assets folder.

  1. When you run the app the first time it sets a shared preference config_run = false.
  2. then i check if config_run = false then parse the xml and dump the data into db
  3. set config_run = true

Now i realize that when i have to push an update on Google Play and add more content into the XML. Even though i change the database version from 1 to 2. The import script wont run because shared preference config_run value will be set to true.

Any pointers on how to handle this situation ?

Scenarios

  1. First Instal - Ver = 1, DB V = 1 (Data is parsed and dumped into the database)
  2. Bugs Fixed and push and update but no data has changed - ver = 1.1, DB V = 1 (It should just replace the code and not upgrade or re-create the database)
  3. Upgraded the DATA and pushed a new update - ver 1.2, DB = 2 ( No new code but data has to be re-created)

The Flow of My App

  1. The App Starts Splash Activity. If Shared Pref - config_run is equal to false then it starts a Progress Dialog and parses and dumps the data into the database.
  2. Upon Parsing and Creating DB and dumping data it goes to MainActivity.

Second Case

  1. SplashActivity Runs and config_run = true so directly goes to MAin Activity.

As Suggested by few people below if i try to dumb the data into the database in onUpgrade of the SQLiteHelper it will happen only in MAinActivity as i dont open a Db connection in the SplashActivity and the Dialog Progress wont be displayed also.

Brisco answered 16/1, 2013 at 15:13 Comment(2)
Where do you parse those assets xml file? In the onCreate method of a SQLiteOpenHelper or manually?Baten
@Luksprog i am doing them right now in my Splash Activity.Brisco
A
4

Instead of setting your shared pref (config_run) to false and making it true, just set the database version into it. When you update your app, check whether you have the same version number in your shared pref. You can do this as shown below:

configRun = settings.getInt("database_version", 0);

if ((DBAdapter.DATABASE_VERSION) == configRun) 
{
//skip xml parsing
}
else
{
//first time configRun will be "0" and DBAdapter.DATABASE_VERSION will be 1
// so you need to parse your xml here and set configRun =1
//on update, change your DB version to 2. now again your configRun and DBAdapter.DATABASE_VERSION will mismatch and you can parse your xml.
}
Angrist answered 24/1, 2013 at 12:43 Comment(0)
W
8

Add a shared pref of the version number you last ran the script for. On app start, check the current apk version, and if newer, run the script again and update the pref

Waterage answered 16/1, 2013 at 15:34 Comment(4)
you mean do it against the version number of the app instead of a config_run right ?Brisco
Yes. Use the android:versionCode="123" from the manifest. To get it from code, see #6594092Waterage
on first run i set the version. then dump the data. next time if the version is the same then it skips. when the app is upgrated it reruns the script.Brisco
when i do it based on version only. i am getting duplicate dataBrisco
S
7

Why dont you want use built in sqlite versioning system. DB version is independed from app version. And it does exactly what you want. SQLiteOpenHelper? Every time tou change your db version an onUpgrate callback will be called and you can refill your db. There are a lot of examples.

Suttles answered 19/1, 2013 at 18:27 Comment(9)
you mean i should parse the xml from with in the sqlitehelper ? can u please point to any example ?Brisco
I mean that if you ship a db with your project there is a built in mechanizm to do db updates. onUpgrade method call when db version changes and you should do something to upgrage it. Why do you add xml to asset? Why you dont want ship prefilled db with app?Suttles
yes i am using that. But using that with the shared pref is getting a bit complicated. I need to figure out a way only to dumb the data and recreate the data when i change content in my xmlBrisco
So change db version in your sqlhelper when you change xml. public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } onUpgrade method will be called and you can drop old data and load new one.Suttles
the parsing and dumping of data doesnt happen from onUpgrade() it happens from the SplashActivity. have updated my question to give u the flowBrisco
so move you parsing to onUpgrade method and create SQLiteHelper in splash activity to update dp while splash is visible. if you still want to use preferences, the right answer have been already given to you by @yoah. delete old data and load new one.Suttles
when in the splash screen and the upgrade is happening. i want to display a progress bar so that the user knows whats happening. thats what i am concerned about for moving it to onUpgrade.Brisco
Plus if a database has to upgrade then. you will need to make a connection to the database. Which doesnt happen in Splash Screen since its not needed. So will i just need to do a dp.open and db.close ?Brisco
Yeah, if you open db (getWritableDatabase will be called some how), onUpgrade method will be called. It is up to you how to implement your db upgrade, but I prefer build in solutions. And SQLiteHelper has one.Suttles
P
4

Have your xmlfiles end with the date of the update, and store the last updated date in sharedpref.

On launch you can check search for updates ( in an optimized way ) and if you find a new file with a new date when compared to the last time you know you need to dump the file.

Total hack job :D

Proteose answered 20/1, 2013 at 9:12 Comment(1)
Thank you for the solution :D i will keep the question open to attract others to give any other solution :D i think this will be a good hack.Brisco
A
4

Instead of setting your shared pref (config_run) to false and making it true, just set the database version into it. When you update your app, check whether you have the same version number in your shared pref. You can do this as shown below:

configRun = settings.getInt("database_version", 0);

if ((DBAdapter.DATABASE_VERSION) == configRun) 
{
//skip xml parsing
}
else
{
//first time configRun will be "0" and DBAdapter.DATABASE_VERSION will be 1
// so you need to parse your xml here and set configRun =1
//on update, change your DB version to 2. now again your configRun and DBAdapter.DATABASE_VERSION will mismatch and you can parse your xml.
}
Angrist answered 24/1, 2013 at 12:43 Comment(0)
S
3

Two things you can do:

  1. The right way: Override database provider's onUpdate() to import the file. (as suggested above)

  2. The one line changer: Instead of check for key="config_run", you check and set for key=("config_run"+DB_VERSION) to see if import is needed, and of course, if the key does not exist, you should return false. This way every time you update the DB number, import job will run again.

This is agnostic to your app version.

Sarthe answered 23/1, 2013 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.