The buffer() operator

Unlike the onBackPressureBuffer() operator, which buffers emissions until the consumer consumes, the buffer() operator will gather emissions as a batch and will emit them as a list or any other collection type.

So, let's look at this example:

    fun main(args: Array<String>) { 
      val flowable = Flowable.range(1,111)//(1) 
      flowable.buffer(10)//(2) 
        .subscribe { println(it) } 
    } 

On comment (1), we created a Flowable instance with the Flowable.range() method, which emits integers from 1 to 111. On comment (2), we used the buffer operator with 10 as the buffer size, so the buffer operator gathers 10 items from the Flowable and emits them as a list.

The following is the output, which satisfies the understanding:

The buffer operator ...

Get Reactive Programming in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.