Hi :blush:, I have a question about coroutine flow...
# coroutines
b
Hi 😊, I have a question about coroutine flow and viewmodel scope. I have a case when I register to a flow in my
onStart()
method in my Fragment A. This flow is attached to the
viewModelScope
. My question is: when I open another fragment and return to my Fragment A
onStart
is called again and the register for the flow is called as well, so the flow is called twice without cancel it as it's attached to the
viewModelScope
and the
onCleared
method was not called yet . How do you handle this case to avoid keeping emitting data to the view when the view is not attached ?
l
Don't use viewModelScope, but the Fragment's viewLifecycleOwner.lifecycleScope instead.
b
@louiscad I'm calling the flow in my interactors from my viewmodel and send state/evetns to my fragment .
viewLifecycleOwner.lifecycleScope
should be used in Fragment, however, in my case I need to use a scope in my viewmodel
l
@Besto You can share the flow in
viewModelScope
, but collect the flow in
viewLifecycleOwner.lifecycleScope