Anyone using Koin & Realm together? I'm getting weird bugs where some queries are out of sync with each - I suspect I've setup DI wrong for opening/using/closing realm. Curious if anyone has a pattern that's working for them?
Christopher Mederos
12/20/2023, 7:12 AM
Copy code
// koin module
fun realmKoinModule() = module {
single { Realm.open(get()) }
single<Configuration> {
RealmConfiguration.Builder(
schema = setOf(
RealmUser::class,
RealmReport::class,
)
).schemaVersion(1).build()
}
single { UsersDb(get()) }
single { ReportsDb(get()) }
}
// Usage
class UsersDb(private val realm: Realm) {
suspend fun createUser(user: User) {
realm.write {
this.copyToRealm(user)
}
}
}
c
Cherrio LLC
12/20/2023, 9:54 PM
Our Koin module is set up as yours.
The difference is we close realm in the
MainActivity
and
ContentView
of respective platforms
Cherrio LLC
12/20/2023, 9:57 PM
On Android:
Copy code
private val realm = get<RealmConfig>()
override fun onDestroy(){
super.onDestroy ()
realm. close()
}