Say, I have a well-built room database in my app.
I planned to do a upgrade for the database. However, the upgrade / data migration is huge, thus I'd like to tell user to be patient, by displaying UI message, such as a AlertDialog
or Toast
, etc.
But I don't know how to check if message is needed to play ---- I mean, how do I get / check their present database version? Is there any function that works like RoomDatabase.getVersion()
, or RoomDatabase.isOlderVersion()
/ RoomDatabase.isOutDated()
?
On the other hand, I know there's Migration
class. Considering it was all I know that can check database version, I was planning to do like
- Create Listener Holder in the Database.
- Before UI Thread build the Database, register a listener in it.
- When
Migration
is called, notify the listener to do things.
However, isn't it to complicated for a version upgrade? Any better idea?
First of all I recommend you to hide all migration in the background thread. It would be probably done anyway if the first access to the database in the app's lifecycle would be performed in the background.
Secondly, if you have any UI thread work with Database, please, try to change the logic of the app to put the access to Database in the background.
If you still need to check the version of the currently running Database this is the place you can get it:
database.openHelper.writableDatabase.version
OR
database.openHelper.readableDatabase.version
Database is the instance of your database you created earlier with the builder.
Btw, how you create the instance?
My question is probably about the automation of the process.
As for me, I create a database instance in the Dagger2. I have the provider method which calls builder. I add migrations there. Also, if the version of the existing database and the version of new database (after update) are different, the builder performs migrations I provide.
If you do all these actions manually, I hope you don't forget about anything.
UPD:
If you would like to know the version even without opening database, I would suggest the most simple way. Store the database version in the Shared Preference
. Get it before building database instance, and perform whatever you would like to before starting the migration.