Hey, I try to understand, I use `instanceKeeper` f...
# mvikotlin
l
Hey, I try to understand, I use
instanceKeeper
for reusing Store and in init I add
lifecycle.doOnDestroy(citiesStore::dispose)
of cause, it doesn’t work after configuration was changed. when should I dispose my store, if I reuse instance via instanceKeeper?
l
yes, I saw it, and in your sample you don’t dispose ListStore when lifecycle call onDestroy
maybe it is overhead, but I want to dispose all my store when lifecycle call onCleared method on ViewModel.
a
It should be destroyed when the related
ViewModel
is cleared. That's the whole purpose of the extension.
So the
Store
supposed to be destroyed when the
Lifecycle
is destroyed and the
Store
is not retained anymore (its
ViewModel
is cleared). But when the
Store
is retained, it should not be disposed.
l
ok go step by step 🙂 in the sample you call todoAddStore::dispose on every doOnDestroy call. but why you don’t call the dispose method for list store?
a
Because the
TodoListStore
is retained, but the
TodoAddStore
is not.
l
I think we don’t have to dispose AddSotre too
a
The it will be leaked?
In the next instance of the controller there will a new instance of the
TodoAddStore
l
yes. but what will leaked?
it is similar to the list store
a
So every
Store
should be destroyed at the end of its own lifecycle.
TodoListStore
has a lifecycle equal to a
ViewModel
. So it is disposed when the
ViewModel
is cleared, normally when the screen is closed. But
TodoAddStore
has a lifecycle equal to screen. So it should be disposed when the screen is destroyed, e.g. when configuration change.
l
I fully agree. But I can’t find place where
So it is disposed when the 
ViewModel
 is cleared
is called
a
When the list screen is rotated, then
TodoListStore
instance is retained - passed to the new instance of the list screen. So it is not disposed at this point. But
TodoAddStore
is not retained. When the new instance of the screen is created, a new instance of
TodoAddStore
is created as well. So previous instance of
TodoAddStore
is disposed.
l
thx. I need to check this point 🙂
👍 1
@Arkadii Ivanov thx for explanations,, i didn’t see dispose in this peace of code
Copy code
getOrCreate { lifecycle ->
        val store = factory()
        lifecycle.doOnDestroy(store::dispose)
        store
    }
a
You are welcome!