The startWith operator

The startWith operator is simple; it enables you to add an item to the producer at the top of all preexisting items.

Let's take a look at how it works:

    fun main(args: Array<String>) { 
      Observable.range(0,10)//(1) 
        .startWith(-1)//(2) 
        .subscribe({ 
           println("Received $it") 
        }) 
     
        listOf("C","C++","Java","Kotlin","Scala","Groovy")//(3) 
          .toObservable() 
          .startWith("Programming Languages")//(4) 
          .subscribe({ 
            println("Received $it") 
          }) 
    }

The output is as follows:

As we can see, the startWith operator on comment (2) and (4) worked just like a prefix on the existing list of emissions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.229.44