Fabian Vorholt
05/18/2022, 3:13 PMparameters
as key in ViewModelComposeExt
:
return remember(qualifier, parameters) {
scope.getViewModel(qualifier, { owner }, parameters)
}
But actually it isn't like that. It returns the same view model with old parameters. Is that behavior intended? Am I missing something?curioustechizen
05/19/2022, 9:36 AMLukasz Kalnik
05/20/2022, 9:39 AMremember
something from Koin?Lukasz Kalnik
05/20/2022, 9:40 AMscope
only returns a new instance when you close and recreate a scopeLukasz Kalnik
05/20/2022, 9:44 AMscope(named("myScope")) {
scoped<ViewModel> {
ViewModel( /* constructor params */ )
}
}
Lukasz Kalnik
05/20/2022, 9:45 AMscoped
for the ViewModel means here "A singleton with the lifetime attached to the scope"Lukasz Kalnik
05/20/2022, 9:49 AMgetKoin().getOrCreateScope(
scopeId = "myScope",
qualifier = named("myScope")
).close()
And then next time you try to access the ViewModel through the same scope it will be a new instance (valid untli you close the scope again):
getKoin().getOrCreateScope(
scopeId = "myScope",
qualifier = named("myScope")
).get<ViewModel>()
(of course it makes sense to create extension functions/properties for getOrCreateScope(...)
to not have to type it again and again)curioustechizen
05/20/2022, 9:51 AMremember
is from Compose. The lambda passed to remember
is re-executed when any of the keys change.
In the original example above, scope.getViewModel
will be called any time qualifier
or parameters
changes.
However, it will not result in a new instance of the ViewModel being returned because like @Lukasz Kalnik mentioned Koin returns the same instance that it created for the active scope.
Disclaimer: This is just my understanding and I could be totally wrong. Would be nice to get a more authoritative answer.Lukasz Kalnik
05/20/2022, 9:52 AMLukasz Kalnik
05/20/2022, 9:54 AMremember
Lukasz Kalnik
05/20/2022, 9:55 AMLukasz Kalnik
05/20/2022, 9:56 AMcurioustechizen
05/20/2022, 10:01 AMFabian Vorholt
05/20/2022, 10:44 AMLukasz Kalnik
05/20/2022, 10:51 AMremember
then? And when is the scope
closed?Fabian Vorholt
05/20/2022, 10:55 AMremember
is used inside the koin compose extension: https://github.com/InsertKoinIO/koin-compose/blob/main/koin-androidx-compose/src/main/java/org/koin/androidx/compose/ViewModelComposeExt.kt#L54
We dont use a custom scope for now. I'll have a look on that nowLukasz Kalnik
05/20/2022, 10:55 AMscope
then you can just remove it. And inject the ViewModel
with a factory
. Then every time you will have a new instance.Lukasz Kalnik
05/20/2022, 10:57 AMLukasz Kalnik
05/20/2022, 10:58 AMby inject()
or get()
. I don't know if it works well in Compose though...Fabian Vorholt
05/20/2022, 11:00 AMIf you don't use a customWhat do you think should be removed?then you can just remove itscope
Lukasz Kalnik
05/20/2022, 11:25 AMscope
Lukasz Kalnik
05/20/2022, 11:26 AMWe dont use a custom scope for now. I'll have a look on that now
Lukasz Kalnik
05/20/2022, 11:27 AMcurioustechizen
05/20/2022, 11:36 AMgetViewModel
which you can call from a Composable.