As far as I know there are three types of buffers in Kotlin flow: Buffer, Conflate and CollectLatest and I'm having trouble figuring out the differences between these three terminal operators.
flow.buffer().collect{...}
flow.collectLatest{...}
flow.conflate().collect{...}
I apologize the brevity, but what are the differences between these buffers and when should we use each of them?
Any help is appreciated in advance.
CodePudding user response:
Normally, emitting and collecting code runs sequentially, "emitter" emits new value after previous one collected somewhere. ("Collected" means terminal operator's lambda is finished). Buffering allows to run emitting code concurrently with collecting code.
buffer() allows emitter to emit new values while the old ones is still being processed (and saves them in buffer for later).
collectLatest() restarts its lambda on each new value collected, even if old one is still being processed.
conflate() operator skips intermediate values, which means that after processing the current value, collector collects only the most recent value received during processing previous one (same as buffer(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST).
CodePudding user response:
Buffers actually accept the same parameters as Channels in addition to specifying the exact capacity of a buffer.
Using the buffer method, you can pass arguments to the capacity parameter to use any type of Channel via buffer(capacity = Channel.<Type>) There is also an onBufferOverflow parameter that can be used to further customize your buffer.
The documentation at https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/buffer.html#:~:text=documentation for details.-,Parameters,-capacity describes your options for creating new buffers.
A good explanation of Channel types can be found on Kotlin's official website:
https://play.kotlinlang.org/hands-on/Introduction to Coroutines and Channels/08_Channels
buffer() allows for multiple coroutines to process emit calls concurrently, thereby saving time instead of waiting for each emit call in sequence.
conflate() is equivalent to buffer(capacity = Channel.CONFLATED) or buffer(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
In the event of multiple emit calls, it will still process them all, but will only collect the final call.
collectLatest(), in the event of multiple emit calls, will not process intermediate values.
It will instead cancel each subsequent call as a new one is made available to it; returning only the final result. This gives it the fastest processing time in situations where you only need the most up-to-date call from the Flow.
