Cold Observables

Take a careful look at all the previous examples. In all the examples, if you subscribe to the same Observable multiple times, you will get the emissions from the beginning for all the subscriptions. Don't believe it? Take a look at the following example:

    fun main(args: Array<String>) { 
      val observable: Observable<String> = listOf      ("String 1","String 2","String 3","String 4").toObservable()//1 
 
      observable.subscribe({//2 
        println("Received $it") 
      },{ 
        println("Error ${it.message}") 
      },{ 
        println("Done") 
      }) 
 
      observable.subscribe({//3 
        println("Received $it") 
      },{ 
        println("Error ${it.message}") 
      },{ 
        println("Done") 
     }) 
    } 

Here is its output:

The program is quite straightforward. Declared an Observable on comment 1, subscribed to the ...

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.