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

Understanding backpressure

Throughout this book, I emphasized the "push-based" nature of Observables. Pushing items synchronously and one at a time from the source all the way to the Observer is indeed how Observable chains work by default without any concurrency.

For instance, the following is an Observable that will emit the numbers 1 through 999,999,999. It will map each integer to a MyItem instance, which simply holds it as a property. But let's slow down the processing of each emission by 50 milliseconds in the Observer. This shows that even if the downstream is slowly processing each emission, the upstream synchronously keeps pace with it. This is because one thread is doing all the work:

 import io.reactivex.Observable;

 public class Launcher {

     public static void main(String[] args) {

         Observable.range(1, 999_999_999)
                 .subscribe(myItem -> {