Repository navigation
Replies: 2 comments
|
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>()) |
|
There are two different lifetimes involved here: the Koin scope that resolves dependencies, and the AndroidX Closing a Koin scope does not, by itself, clear the owner's Also, For this database-replacement case, I would make restoration an explicit application transition:
Calling the correct owner's The Room invalidation suggestion above doesn't apply to replacing your SQLDelight driver: an existing query flow is still associated with its old query/driver objects. A getter such as I checked the Koin implementation linked above; I haven't run your app's restore sequence. The key thing to inspect is the actual |
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!
All reactions