Hi all, new Koin on Android user here. In Dagger, ...
# koin
l
Hi all, new Koin on Android user here. In Dagger, I would create a Singleton AppComponent Room Database which I would prepopulate in
.addCallback(object : RoomDatabase.Callback() { override fun onCreate(db: SupportSQLiteDatabase) ... }
. So by the time I needed to use the data inserted, it was there. With Koin, my DB is declared as a
bean
and it gets created lazily so first time I access the database, 0 items is returned, coming back to that screen again renders all of the items. I tried initializing the DB in my Application.onCreate() but the the database's onCreate() does not get called. Any ideas what can be done? My thinking is I need to somehow create the database and its data before it's first accessed (as opposed to how Koin is designed to access dependencies lazily when they are needed by that particular screen). I doubt this part is relevant but I use Google Arch Paging component to load data.
d
So you want your DB object to be eagerly instantiated instead of lazily?
single<Service>(createOnStart=true) { TestServiceImp() }
l
Yes, perfect. I will try it out!
That did not seem to help
d
What does your whole bean declaration look like?
l
Copy code
val dbModule = module(createOnStart=true) {
        single { MyDatabase.getInstance(androidContext()) }
        single { get<MyDatabase>().myDao() }
    }

    val otherModule = module {
        single { MyRepositoryImpl(get(), get()) as MyRepository }
        single { MyKeyedDataSource(get()) }
        single { MyPagingDataSourceFactory(get()) }
        single { MyViewModelFactory(get()) }
    }
and using it in my Application onCreate()
startKoin(this, listOf(dbModule, otherModule))
d
interesting - I didn’t know you could put createOnStart on a module. have you tried putting it on the specific bean that needs to be created immediately to seed data?
l
yep, same result
Copy code
val dbModule = module {
        // eager creation for this definition
        single(createOnStart=true) { MyDatabase.getInstance(androidContext()) }
        single(createOnStart=true) { get<MyDatabase>().myDao() }
    }
i am going to try something similar but with a smaller todo app with paging
this worked in a smaller app so i'll have to dig deeper. thanks for your help!
hmm, upon closer look, same problem with the todoapp