Understanding multicasting
We have used the ConnectableObservable
earlier in Chapter 2, Observables and Subscribers. Remember how cold Observables, such as Observable.range()
, will regenerate emissions for each Observer
? Let's take a look at the following code:
import io.reactivex.Observable; public class Launcher { public static void main(String[] args) { Observable<Integer> threeIntegers = Observable.range(1, 3); threeIntegers.subscribe(i -> System.out.println("Observer One: " + i)); threeIntegers.subscribe(i -> System.out.println("Observer Two: " + i)); } }
The output is as follows:
Observer One: 1 Observer One: 2 Observer One: 3 Observer Two: 1 Observer Two: 2 Observer Two: 3
Here, Observer One
received all three emissions and called onComplete()
. After that, Observer Two
received the three emissions (which were regenerated again) and called onComplete()
. These were two separate streams of data generated for two separate subscriptions. If we...