Automatic connection
There are definitely times you will want to manually call connect()
on ConnectableObservable
to precisely control when the emissions start firing. There are convenient operators that automatically call connect()
for you, but with this convenience, it is important to have awareness of their subscribe timing behaviors. Allowing an Observable
to dynamically connect can backfire if you are not careful, as emissions can be missed by Observers.
autoConnect()
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...