The viewModelScope is using Dispatchers.Main.immediate since 2.2.0-alpha04
In the case of immediate dispatching (main thread) and couple with a LiveDataOwner only the last MutableLiveData is emitted.
fun initialize() {
flow {
//Works with delay(1) or changing dispatcher
emit("first")
emit("second")
}
.onEach { mutableState.value = it }
.launchIn(viewModelScope) //Works with Dispatchers.Main
}
myViewModel
.state
.observe(this, Observer { //Works when observing for ever
linear.addView(TextView(this).apply { text = it })
//only last emitted element is catch
//Result is: "second"
//Expected: "first, second"
})
myViewModel.initialize()