I was trying to pass events from UI to viewModel using sharedFlow this is my viewmodel class
class MainActivityViewModel () : ViewModel() {
val actions = MutableSharedFlow<Action>()
private val _state = MutableStateFlow<State>(State.Idle)
val state: StateFlow<State> = _state
init {
viewModelScope.launch { handleIntents() }
}
suspend fun handleIntents() {
actions.collect {
when (it) {...}
}
}
}
and this is how i am emiting actions
private fun emitActions(action: Action) {
lifecycleScope.launch {
vm.actions.emit(action)
}
}
For the first time emission happening as expected, but then it is not emitting/collecting from the viewmodel.
Am i doing anything wrong here??
When I used collectLatest()
instead of collect()
it worked as expected