I read this article yesterday : <https://medium.co...
# android
j
I read this article yesterday : https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda. I’m wondering in the case of using this in a view model
Copy code
val state: StateFlow<MyViewState> = myFlow
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), MyViewState())
does one stills need to use something like this from a view
Copy code
lifecycleOwner.addRepeatingJob(Lifecycle.State.STARTED) {
   viewModel.state.collect {
        // do something with the data
    } 
}
i’m not completely sure what
SharingStarted.WhileSubscribed()
implies regarding android and its life cycle
m
This is one of the cases you should definitely use the new APIs. In the VM layer, you don’t know what
myFlow
is or how it’s implemented.
WhileSubscribed()
means that when there are no collectors available, the underlying flow will be cancelled.
If for some reason, you 100% want to keep the underlying producer active, use the new APIs, but change the
WhileSubscribed
to
Eagerly
or
Lazily
j
alright, thanks for the quick answer 🙂
👍 1
r
what if you convert the state flow in the example above to LiveData, and then you observe it from the Fragment
would that mean you dont need the new API's ?