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)

Once and only once


An emitting Observable sequence could emit duplicates by error or by design. The distinct() and distinctUntilChanged() functions allow us to deal smoothly with duplicates.

Distinct

What if we want to be absolutely sure that we process a specific value only once? We can get rid of the duplicates by applying the distinct() function to our Observable sequence. Like takeLast(), distinct() works with a completed sequence and to achieve this duplication filtering, it needs to keep a track of every emitted item. This is a memory usage concern that you have to keep in mind if you are dealing with a huge sequence or big emitted items.

The next figure shows how we can create a sequence with no duplicate working on an Observable source that emits numbers and emits 1 and 2 twice:

To create our example sequence, we are going to use a few methods we have learned so far:

  • take(): This is to have a small set of recognizable items

  • repeat(): This is to create a larger sequence containing a few...