O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ReplaySubject

ReplaySubject takes an entirely different approach compared to Publish and BehaviorSubject instances . It records all the values that it received and, upon a new subscription to itself, it will replay all the captured values to the new subscription.

Take a look here:

Subject<String> subject = ReplaySubject.create();Observable.interval(0, 1, TimeUnit.SECONDS)        .map(Objects::toString)        .subscribe(subject);Thread.sleep(3100);subject.subscribe(v -> log(v));

In this block, we have created a ReplaySubject with the following:

Subject<String> subject = ReplaySubject.create();

Also, we have made it receive values from Observable.interval():

Observable.interval(0, 1, TimeUnit.SECONDS)        .map(Objects::toString)        .subscribe(subject);

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required