Anyone using Koin & Realm together? I'm gettin...
# multiplatform
c
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?
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
Our Koin module is set up as yours. The difference is we close realm in the
MainActivity
and
ContentView
of respective platforms
On Android:
Copy code
private val realm = get<RealmConfig>()

override fun onDestroy(){
   super.onDestroy ()
   realm. close()
}
👍 1