Hi! I noticed that ScopeDLS.viewModel (...) extens...
# koin
a
Hi! I noticed that ScopeDLS.viewModel (...) extension uses “scoped” instead of “factory” for the ViewModel definition. This fact may lead to issues if the scope qualifier is different from the ViewModelStoreOwner. Here is an example: Let’s create scopes:
scope(qualifier<SomeFragment>())
scope(qualifier<LongLiveScope>()) {
  
viewModel { SomeViewModel ()}
}
Then we link these scopes:
fragmentScope.linkTo(longLiveScope)
Then we inject SomeViewModel into SomeFragment with the
viewModel()
extension, which uses the ViewModelStore from SomeFragment. When SomeFragment is destroyed, its ViewModelStore is also destroyed, and
SomeViewModel::onClear
will be called. However, the SomeViewModel instance will not be destroyed because it is scoped in LongLiveScope, which leads to issues when the same (already cleared) instance of SomeViewModel is reused in new fragments. For example, ViewModel’s coroutines scope will not work because it will be already closed after onCleared. I think the ViewModel should be shared using the shared ViewModelStore, but not using the Koin scope, otherwise, it breaks the lifecycle of the ViewModel itself. I hope I explained this issue in sufficient detail. Can you please explain why the ViewModel definition has changed? And what can I do if the ViewModelStoreOwner is different from the scope, other than rolling back the old ViewModel definition? Thank you!
a
viewModel in scope can survive to lifecycle changem and then regenerates the instance from current Android ViewModel Factory
as previously it was loosing it completely
referencing ViewModel in cascade this way, is not sometyhing I would advise