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)

Preface

In a world where there is a smartphone in every pocket, designing and building applications that can run smoothly and provide a user experience that users will enjoy is the only way to go. The reactive programming style with RxJava will help you beat Android Platform limitations to create astonishing Android Apps.

This book will be a practical journey, from the basics of reactive programming and Observer pattern concepts, to the main feature of RxJava, which will be accompanied by practical code examples and a real-world app.

I'll show you how to create an Observable from "scratch", from a list, or from a function that we already have in our codebase. You will learn how to filter an Observable sequence to create a new sequence, containing only the values we want; you will also learn how to apply a function to an Observable and how to concatenate, merge, or zip Observables. I'll show you how to enjoy RxAndroid Schedulers to overcome the threading and concurrency hell in Android.

The book will wind up with a practical example of RxJava combined with Retrofit to easily communicate with a REST API.

What this book covers

Chapter 1, RX – from .NET to RxJava, takes initial steps into the reactive world. We will compare the reactive approach with the classic approach, and will explore the similarities and differences between them.

Chapter 2, Why Observables?, gives an overview of the Observer pattern, how it's implemented and extended by RxJava, what an Observable is, and how Observables relate to Iterables.

Chapter 3, Hello Reactive World, uses what we have learned so far to create our first reactive Android app.

Chapter 4, Filtering Observables, dives into the essence of an Observable sequence: filtering. We will also learn how to select only the values we want from an emitting Observable, how to obtain a finite number of values, how to handle overflow scenarios, and a few more useful tricks.

Chapter 5, Transforming Observables, shows how to transform Observable sequences to create sequences that can fit our needs.

Chapter 6, Combining Observables, digs into combining functions, and we are going to learn how to work with multiple Observables simultaneously when we create the Observable we want.

Chapter 7, Schedulers – Defeating the Android MainThread Issue, shows you how to work with multithreading and concurrent programming using RxJava Schedulers. We will create network operations, memory accesses, and time-consuming tasks in a reactive way.

Chapter 8, REST in Peace – RxJava and Retrofit, teaches you how Retrofit by Square can be used with RxJava to create an REST client efficiently and effectively.

What you need for this book

To run the examples in this book, you will need a standard Android development environment:

  • Android Studio or IntelliJ IDEA

  • The Android SDK

  • The Java JDK

As you are approaching RxJava as a pure Java developer, you will obviously need your preferred Java editor and a standard Java JDK environment. Some of the figures used in this book are taken from http://rxmarbles.com/ and http://reactivex.io/.

Who this book is for

If you are an experienced Java developer, reactive programming will give you a new way to approach scalability and concurrency in your backend systems, without forcing you to switch to change the programming languages. This book will help you learn the core aspects of RxJava and will also help you overcome the limitations of the Android platform to create event-driven, reactive, and smooth Android applications.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "As you can see, zip() has three parameters: the two Observables and Func2, as expected."

A block of code is set as follows:

public Observable<List<User>> getMostPopularSOusers(int howmany) {
        return mStackExchangeService
              .getMostPopularSOusers(howmany)
                .map(UsersResponse::getUsers)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public Observable<List<User>> getMostPopularSOusers(int howmany) {
        return mStackExchangeService
              .getMostPopularSOusers(howmany)
                .map(UsersResponse::getUsers)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "We will just need a fancy progress bar and a DOWNLOAD button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/9108OS_GraphicsBundle.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.