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

Using to() for fluent conversion

On rare occasions, you may find yourself having to pass an Observable to another API that converts it into a proprietary type. This can be done simply by passing an Observable as an argument to a factory that does this conversion. However, this does not always feel fluent, and this is where the to() operator comes in.

For example, JavaFX has a Binding<T> type that houses a mutable value of type T, and it will notify affected user interface elements to update when it changes. RxJavaFX has JavaFxObserver.toBinding() and JavaFxSubscriber.toBinding() factories, which can turn an Observable<T> or Flowable<T> into a JavaFX Binding<T>. Here is a simple JavaFX Application that uses Binding<String> built-off Observable<String>, which is used to bind to a textProperty() operator of label:

 import io.reactivex.Observable;
 import io.reactivex.rxjavafx.observers.JavaFxObserver;
 import io.reactivex.rxjavafx.schedulers.JavaFxScheduler...