Replies: 1 comment
-
|
To "reset" ViewModels in Koin after a major data event like a database restoration, I'd suggest these two approaches: Approach A: Reactive UI (Preferred)If your Repository exposes data as a Approach B: Custom Koin ScopesIf you need to force-recreate the ViewModel instances, you can define them within a custom Koin Scope. This allows you to close and re-open the scope, which wipes the cached instances. Approach 1: Define the Scope val myModule = module {
scope<MySessionScope> {
viewModel { MyViewModel(get()) }
}
}Approach 2: Reset Logic getKoin().getScopeOrNull("session_id")?.close()
val newScope = getKoin().createScope("session_id", named<MySessionScope>()) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I added functionality to restore local database backups (sqldelight).
When restoring, I close the old db driver, re-create it.. but the viewmodels are likely keeping a reference to the old db alive, via observers, e.g:
with dao:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/foo/databases/db.db), msg: UiErrorMsg(msg=Unknown error, debugInfos=java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/foo/databases/db.db)
So I figured, I'd like to tear down and recreate all the view models? but not succeeding so far.
I defined a scope
and then
koin.getScopeOrNull("viewmodels")?.close()but e.g. this on viewmodels is never called
and error persists.
there's also e.g.
but not sure this is what I should use and how to combine with the view models management by koin?
Maybe I'm entirely off track and I need a different approach to solve this, any advice appreciated!
Beta Was this translation helpful? Give feedback.
All reactions