The startWith operator

We got introduced to the startWith operator in the previous chapter, but there's still a lot to cover. This operator also lets you combine multiple producers. Take a look at the following example:

    fun main(args: Array<String>) { 
      println("startWith Iterator") 
      Observable.range(5,10) 
        .startWith(listOf(1,2,3,4))//(1) 
        .subscribe { 
           println("Received $it") 
        } 
        println("startWith another source Producer") 
       Observable.range(5,10) 
         .startWith(Observable.just(1,2,3,4))//(2) 
         .subscribe { 
            println("Received $it") 
         } 
   } 

We can pass another source Observable or an Iterator instance to be prepended before the source Observable that the operator has subscribed to starts emitting.

In the preceding program, on comment (1), we used the startWith ...

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.