The New Problem
Room, however, does not use SQLiteOpenHelper
… at least, not directly. There is no obvious place that you can use to put SQLiteAssetHelper
.
There are two potential hooks that we saw in the chapter on the support database API.
One is with addCallback()
on RoomDatabase.Builder
. This allows you to register a RoomDatabase.Callback
instance with the RoomDatabase
. That callback will be called with onCreate()
when the database is created for the first time. If you wanted to execute some SQL statements to populate the database, you could use this approach. However, Room stores its own metadata in the database in a private table. We have no great way of putting that metadata inside our pre-populated database, and by the time onCreate()
is called, it is too late for us to try to swap in that pre-populated database.
The other hook is with openHelperFactory()
on RoomDatabase.Builder
. This is the entry point to replacing the standard SQLite access code with our own code. As it turns out, a slightly-modified version of SQLiteAssetHelper
can be used in this fashion.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.