June 2017
Intermediate to advanced
400 pages
8h 44m
English
The autoConnect() operator on ConnectableObservable can be quite handy. For a given ConnectableObservable<T>, calling autoConnect() will return an Observable<T> that will automatically call connect() after a specified number of Observers are subscribed. Since our previous example had two Observers, we can streamline it by calling autoConnect(2) immediately after publish():
import io.reactivex.Observable;import java.util.concurrent.ThreadLocalRandom;public class Launcher { public static void main(String[] args) { Observable<Integer> threeRandoms = Observable.range(1,3) .map(i -> randomInt()) .publish() .autoConnect(2); //Observer 1 - print each random integer threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i
Read now
Unlock full access