lehakorshun
09/03/2020, 4:09 PMinstanceKeeper
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?Arkadii Ivanov
09/03/2020, 4:25 PMStore
you can use getOrCreateStore
extension function: https://github.com/arkivanov/MVIKotlin/blob/41a47c0fb022bc79312053d7605f83a02e1b5df7/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/instancekeeper/InstanceKeeperExt.kt#L33
• To retain an arbitrary object you can use getOrCreate
extension function: https://github.com/arkivanov/MVIKotlin/blob/41a47c0fb022bc79312053d7605f83a02e1b5df7/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/instancekeeper/InstanceKeeperExt.kt#L17 . It provides you a wrapped Lifecycle
that is destroyed then when the object is completely gone.lehakorshun
09/03/2020, 4:33 PMlehakorshun
09/03/2020, 4:36 PMArkadii Ivanov
09/03/2020, 4:45 PMViewModel
is cleared. That's the whole purpose of the extension.Arkadii Ivanov
09/03/2020, 4:46 PMArkadii Ivanov
09/03/2020, 4:48 PMStore
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.lehakorshun
09/03/2020, 4:48 PMArkadii Ivanov
09/03/2020, 4:50 PMTodoListStore
is retained, but the TodoAddStore
is not.lehakorshun
09/03/2020, 4:52 PMArkadii Ivanov
09/03/2020, 4:52 PMArkadii Ivanov
09/03/2020, 4:53 PMTodoAddStore
lehakorshun
09/03/2020, 4:53 PMlehakorshun
09/03/2020, 4:55 PMArkadii Ivanov
09/03/2020, 4:55 PMStore
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.lehakorshun
09/03/2020, 4:56 PMSo it is disposed when theis calledis clearedViewModel
Arkadii Ivanov
09/03/2020, 4:56 PMTodoListStore
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.Arkadii Ivanov
09/03/2020, 5:00 PMgetOrCreate
extension method provides another Lifecycle
, which is destroyed when the ViewModel
is cleared: https://github.com/arkivanov/MVIKotlin/blob/41a47c0fb022bc79312053d7605f83a02e1b5df7/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/instancekeeper/InstanceKeeperExt.kt#L17
Each InstanceKeeper
has a Lifecycle
https://github.com/arkivanov/MVIKotlin/blob/41a47c0fb022bc79312053d7605f83a02e1b5df7/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/instancekeeper/InstanceKeeper.kt#L15lehakorshun
09/03/2020, 5:02 PMlehakorshun
09/03/2020, 5:29 PMgetOrCreate { lifecycle ->
val store = factory()
lifecycle.doOnDestroy(store::dispose)
store
}
Arkadii Ivanov
09/03/2020, 5:33 PM