Let's make this simple. I've one MutableSharedFlow
named sortOrder
in my ViewModel
.
private val sortOrder = MutableSharedFlow<String>(
replay = 0,
extraBufferCapacity = 1
)
I've a mapLatest
connected to the sortOrder
to refresh data whenever the sortOrder
changed.
val data = sortOrder.mapLatest {
Timber.d("Sort order changed to $it")
"Sort order is $it"
}
I have an observer listening to data
in the activity.
viewModel.data.asLiveData().observe(this) {
Timber.d("onCreate: New data is $it")
}
and finally, I change the sortOrder
in ViewModel
's init
method
init {
sortOrder.tryEmit("year")
}
but even after changing the sortOrder
value, the mapLatest
not getting triggered. Any idea why?
I am using MutableSharedFlow
to control the replay
property to prevent executing mapLatest
body every time I rotate the screen (or when the activity recreated).
Full Source Code
PS: I am new to Flow APIs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…