Book Image

RxJava Essentials

By : Ivan Morgillo
Book Image

RxJava Essentials

By: Ivan Morgillo

Overview of this book

<p>RxJava—Reactive Extensions for the JVM—is a library for composing asynchronous and event-based programs using Observable sequences for the Java VM, which will help you beat Android platform limitations to create astonishing Android apps.</p> <p>Starting with some quick background information on the Rx .NET library, this book quickly moves on to your first example. You will understand Observables and learn to filter, transform, or merge them in detail. Next, you will learn how to get rid of Threads, AsyncTasks, and Handlers with Schedulers to create a smooth user experience. Develop an easy, ready-to-go approach to REST API communications and enrich your skills by working with new challenging examples.</p> <p>By the end of the book, you will have explored the reactive programming world and will have created your first Android app without having to think about threading, networking, concurrency, and collection management.</p> <p>The images have been taken from&nbsp;<a href="http://reactivex.io/" target="_blank">http://reactivex.io/</a> which is licensed under a Create Commons 3.0 Attribution license (<a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">https://creativecommons.org/licenses/by/4.0/</a>)</p>
Table of Contents (15 chapters)

Microsoft Reactive Extensions


Functional reactive programming is an idea from the late 90s that inspired Erik Meijer, a computer scientist at Microsoft, to design and develop the Microsoft Rx library.

Rx is a reactive extension for Microsoft .NET. Rx provides an easy way to create asynchronous, event-driven programs using Observable sequences. A developer can model an asynchronous data stream using Observables, query the Observables using LINQ syntax, and easily manage concurrency with Schedulers.

Rx makes well-known concepts, such as the push approach, easy to implement and consume. In a reactive world, we can't just wait for a function result, a network call, or a database query to return and pretend that the user won't notice or even complain about it. Every moment we wait for something, we lose the opportunity to do other things in parallel, provide a better user experience, and free our software from the chains of sequential, blocking programming.

.NET Observable relates to .NET Enumerable according to the following table:

.NET Observable

Single return value

Multiple return values

Pull/Synchronous/Interactive

T

IEnumerable<T>

Push/Asynchronous/Reactive

Task<T>

IObservable<T>

The push approach reverses the problem: instead of asking for a result and waiting, the developer simply asks for a result and gets notified when the result is available. The developer provides a clear sequence of reactions to the events that are going to happen. For every event, the developer provides a reaction; for example, the user is asked to log in and submit a form with his username and password. The application executes the network call for the login and states what is going to happen:

  • Show a success message and store the user's profile

  • Show an error message

As you can see with the push approach, the developer doesn't wait for the result. He will be notified when the result arrives. In the meantime, he can do whatever he wants:

  • Show a progress dialog

  • Store the username and password for future logins

  • Preload something he knows would take some time the moment the login succeeds