I'm trying to create a cold `Flow` where a lot of ...
# coroutines
m
I'm trying to create a cold
Flow
where a lot of updates can happen at once and I want the collector to only process the last one. The producer and the consumer are both on the UI thread. The desired behavior is when the producer gets out of their loop and goes back to waiting for some event, then the consumer gets the latest state. What I have now is a
callbackFlow
that is
conflated
.
Copy code
val flow = callbackFlow<Any> {
    event.register { offer(someValue) }
    awaitClose { event.unregister() }
}.conflate()
Copy code
val job = scope.launch() {
   flow.collect { }
The scope is using Android's
MainScope
. And the event is sending events on the main thread. When I was using a
MutableStateFlow
this behaves as I wanted, but with the conflated
callbackFlow
the caller is getting the first and last event, once all the events have been offerred.
b
m
I don't want to just process the last, just skip any events that are not the latest by the time the collector can process them. My understanding is that is what
conflate
does, but it is getting me the first and last, and skipping the ones in the middle.
b
Have you looked into
collecteLatest
?
m
I'll try
didn't change the behavior