Looping through all emissions - blockingForEach

If you want to loop through all the emissions then blockingForEach is probably a better solution. It's better than blockingIterable as it will not queue up the emissions. Rather will it block the calling thread and wait for each emission to be processed before allowing the thread to continue.

In the following example, we created an Observable from a list of Int. Then applied a filter for even numbers only and then within the blockingForEach we are testing whether all the received numbers are even:

    @Test 
    fun `test with blockingForEach`() { 
      val list =   listOf(2,10,5,6,9,8,7,1,4,3,12,20,15,16,19,18,17,11,14,13) val observable = list.toObservable() .filter { item -> item%2==0 } observable.forEach ...

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.