https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

mkrussel

04/30/2021, 3:01 PM
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

baxter

04/30/2021, 6:20 PM
m

mkrussel

04/30/2021, 8:20 PM
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

baxter

04/30/2021, 8:40 PM
Have you looked into
collecteLatest
?
m

mkrussel

04/30/2021, 8:49 PM
I'll try
didn't change the behavior
7 Views