Android Room with Hilt - How to populate a table after database creation?

Viewed 407

I have a Hilt Dependency Injection that creates a database like so :

@Module
@InstallIn(ApplicationComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideRunningDatabase(
        @ApplicationContext app: Context
    ) = Room.databaseBuilder(
        app,
        WordDb::class.java,
        Constants.WORD_DATABASE_NAME
    ).build()

    @Singleton
    @Provides
    fun provideWordDao(db: WordDb) = db.wordDao()
}

Is there an elegant way to populate the database after creation? By elegant I mean not doing it in a onCreate() by means of a Preference.

I know one can setup a callback in databaseBuilder(), but I can't figure out how to do it with Hilt.

0 Answers
Related