Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

393
Views
Difference between buffers in Kotlin flow

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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).

over 4 years ago · Santiago Trujillo Report

0

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%20for%20details.-,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%20to%20Coroutines%20and%20Channels/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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!