I am trying to ensure that my database always contains an initial row. I read through How to populate Android Room database table on first run? and the main thing that I'm running into is that I have no instance to access (or I don't know how to access it?) using Hilt when I'm creating the database. If I try re-using the provideDatabase
Hilt method I've written, it results in SQLite Database Leaks (presumably because there's no one around to close the database using those spawned instances). Here's my code:
@Module
@InstallIn(ApplicationComponent::class)
object AppModule {
@Singleton
@Provides
fun provideDatabase(@ApplicationContext context: Context): GameDatabase {
return Room.databaseBuilder(context, GameDatabase::class.java, GameDatabase.GAME_DB_NAME)
.addCallback(
object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
// Initialize the database with the first game
ioThread {
provideDatabase(context).gameDao().createNewGame(Game())
}
}
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
// Ensure there is always one game in the database
// This will capture the case of the app storage
// being cleared
// THIS results in an instance being created that can't be closed - causing DB leaks!
ioThread {
val gameDao = provideDatabase(context).gameDao()
if (gameDao.gameCount() == 0) {
gameDao.createNewGame(Game())
}
}
}
}
).build()
}
@Singleton
@Provides
fun provideGameDao(database: GameDatabase): GameDao {
return database.gameDao()
}
}
So, how do I get a hold of my DAO to do the initialization? Do I need to just manually craft an insert statement in SQL and call it on the database?