June 2017
Intermediate to advanced
400 pages
8h 44m
English
ReplaySubject is similar to PublishSubject followed by a cache() operator. It immediately captures emissions regardless of the presence of downstream Observers and optimizes the caching to occur inside the Subject itself:
import io.reactivex.subjects.ReplaySubject;import io.reactivex.subjects.Subject;public class Launcher { public static void main(String[] args) { Subject<String> subject = ReplaySubject.create(); subject.subscribe(s -> System.out.println("Observer 1: " + s)); subject.onNext("Alpha"); subject.onNext("Beta"); subject.onNext("Gamma"); subject.onComplete(); subject.subscribe(s -> System.out.println("Observer 2: " + s)); }}
The output is as follows:
Observer 1: AlphaObserver 1: BetaObserver 1: GammaObserver 2: Alpha ...
Read now
Unlock full access