Book Image

Learning RxJava

By : Thomas Nield
Book Image

Learning RxJava

By: Thomas Nield

Overview of this book

RxJava is a library for composing asynchronous and event-based programs using Observable sequences for the JVM, allowing developers to build robust applications in less time. Learning RxJava addresses all the fundamentals of reactive programming to help readers write reactive code, as well as teach them an effective approach to designing and implementing reactive libraries and applications. Starting with a brief introduction to reactive programming concepts, there is an overview of Observables and Observers, the core components of RxJava, and how to combine different streams of data and events together. You will also learn simpler ways to achieve concurrency and remain highly performant, with no need for synchronization. Later on, we will leverage backpressure and other strategies to cope with rapidly-producing sources to prevent bottlenecks in your application. After covering custom operators, testing, and debugging, the book dives into hands-on examples using RxJava on Android as well as Kotlin.
Table of Contents (21 chapters)
Title Page
About the Author
About the Reviewers
Customer Feedback

Replaying and caching

Multicasting also allows us to cache values that are shared across multiple Observers. This may sound surprising, but when you think about it long enough, you may realize this makes sense. If we are sharing data across multiple Observers, it makes sense that any caching feature would be shared across Observers too. Replaying and caching data is a multicasting activity, and we will explore how to do it safely and efficiently with RxJava.


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,...