https://kotlinlang.org logo
Title
l

lehakorshun

09/03/2020, 4:09 PM
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

lehakorshun

09/03/2020, 4:33 PM
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

Arkadii Ivanov

09/03/2020, 4:45 PM
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

lehakorshun

09/03/2020, 4:48 PM
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

Arkadii Ivanov

09/03/2020, 4:50 PM
Because the
TodoListStore
is retained, but the
TodoAddStore
is not.
l

lehakorshun

09/03/2020, 4:52 PM
I think we don’t have to dispose AddSotre too
a

Arkadii Ivanov

09/03/2020, 4:52 PM
The it will be leaked?
In the next instance of the controller there will a new instance of the
TodoAddStore
l

lehakorshun

09/03/2020, 4:53 PM
yes. but what will leaked?
it is similar to the list store
a

Arkadii Ivanov

09/03/2020, 4:55 PM
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

lehakorshun

09/03/2020, 4:56 PM
I fully agree. But I can’t find place where
So it is disposed when the 
ViewModel
 is cleared
is called
a

Arkadii Ivanov

09/03/2020, 4:56 PM
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

lehakorshun

09/03/2020, 5:02 PM
thx. I need to check this point 🙂
👍 1
@Arkadii Ivanov thx for explanations,, i didn’t see dispose in this peace of code
getOrCreate { lifecycle ->
        val store = factory()
        lifecycle.doOnDestroy(store::dispose)
        store
    }
a

Arkadii Ivanov

09/03/2020, 5:33 PM
You are welcome!