Replaying

The replay() operator is a powerful way to hold onto previous emissions within a certain scope and re-emit them when a new Observer comes in. It will return a ConnectableObservable that will both multicast emissions as well as emit previous emissions defined in a scope. Previous emissions it caches will fire immediately to a new Observer so it is caught up, and then it will fire current emissions from that point forward.

Let's start with a replay() with no arguments. This will replay all previous emissions to tardy Observers, and then emit current emissions as soon as the tardy Observer is caught up. If we use Observable.interval() to emit every second, we can call replay() on it to multicast and replay previous integer emissions. ...

Get Learning RxJava now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.