repopulating room database by deleting all the content

Viewed 17

I now finished the android codelab about room db with MVVM arch. But there is one part that i didn't exactly understand. This is a sentence from the codelab:

To delete all content and repopulate the database whenever the app is created, you'll create a RoomDatabase.Callback and override onCreate().

and this is the code they provide:

private class WordDatabaseCallback(
    private val scope: CoroutineScope
) : RoomDatabase.Callback() {

    override fun onCreate(db: SupportSQLiteDatabase) {
        super.onCreate(db)
        INSTANCE?.let { database ->
            scope.launch {
                populateDatabase(database.wordDao())
            }
        }
    }

    suspend fun populateDatabase(wordDao: WordDao) {
        // Delete all content here.
        wordDao.deleteAll()
    }
}

the part that I don't understand is "deleting all content". why do I need to delete all content when the app is created? and what do they mean by "whenever the app is created"? is it for the first time the app installed or everytime app is opened?

when I don't use this code, the app works fine too. can someone explain the purpose of deleting everything?

1 Answers

why do I need to delete all content when the app is created?

You don't need to, this is a specific scenario where for whatever reason you want the App to delete all content stored in the database. However there should not be any anyway as onCreate is only called when the database doesn't actually exist (unless manually invoked), so there will be no content to delete.

  • If you used a pre-packaged database using the .createFromAsset then onCreate isn't called.

  • I believe that they have just included a simple, unlikely to fail, introduction to using a Callback.

and what do they mean by "whenever the app is created"? is it for the first time the app installed or everytime app is opened?

The former. That is the whole purpose of a Database is to store data long term. As such the database is stored in the App's data space. When an App is installed then the data space will not have the database. So the App and thus Room has to know to create the database before it can be used.

So when you attempt to use the database, the processing of preparing to use the database checks to see if the database exists.

  • If the database does exist then it carries on without calling onCreate.

  • If the database does not exist then Room will try to create the database and the tables therein via the onCreate method. The Callback allows intervention at this stage by overriding the onCreate method.

  • If the App is stopped and rerun the database will still exist, and onCreate is not called.

  • If the App is uninstalled and then re-installed the the database will be deleted, so onCreate is called.

  • If a new version of the App is installed, then the database will still exist and onCreate is not called.

  • If a new version of the App is installed and it includes a new version for the database, onCreate is not called. Instead whatever migration path is specified will be taken.

Related