Am using `StateFlow` in view model (along with use...
# coroutines
j
Am using
StateFlow
in view model (along with use of
stateIn
)....is there something equivalent to
MediatorLiveData
or
switchMap
when using that?
j
combine?
j
The specific example I'm looking at is
Copy code
val network = MutableStateFlow<String>("")
val stations = cityBikesRepository.pollNetworkUpdates(network.value)
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
When
network
changes I want to restart polling using that value....I'm possibly trying to overstretch this approach to something it's perhaps not intended to support
hmm, maybe
combine
might do the trick
j
switchmap is flatMapLatest.
j
yeah, looks like flatMapLatest is what I need.....thanks @Jan Skrasek / @Javier
Copy code
val stations =  network.flatMapLatest {  cityBikesRepository.pollNetworkUpdates(it) }
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
👍 2
got to love Kotlin 🙂
g
You could just try to use switchMap, it exists, but deprecated, it's just an alias for flatMapLatest
j
In original question here I had been thinking of LiveData's
switchMap
but, yeah, could have probably also used Flow's one....in general I was probably overthinking this with use of
StateFlow
in the mix