Book Image

Reactive Android Programming

By : Tadas Subonis
Book Image

Reactive Android Programming

By: Tadas Subonis

Overview of this book

<p>Writing code on Android is hard. Writing a high quality code that involves concurrent and parallel tasks is even harder. Ensuring that this code will run without unforeseen race conditions is an the order of magnitude harder. RxJava is the tool that can help write code for such tasks.</p> <p>In this book a novice developer will be introduced to a wide variety of tools that RxJava provides to enable them to produce robust and high-quality code for their asynchronous tasks by building a relatively simple(and high quality) application using advanced RxJava techniques to produce a high quality product.</p> <p>Part 1 of the book will lead the developer through RxJava's initial setup in Android environment. In Part 2, the reader will learn RxJava 2.0 step-by-step by starting off with stock data processing and display.The developer will learn to choose appropriate Schedulers and to use Retrofit library for remote requests.In Part 3, the reader will also learn advanced topics such as adding integration to Twitter to process its streaming data by combining it with stock data.</p>
Table of Contents (19 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Free Chapter
1
Building your First “Hello World” RxJava Application

Combining items


It can happen quite often that there is a need to execute two asynchronous tasks and wait for the result of both of them before the next step can be taken.

For example, consider downloading data for two stocks to compare their prices. The fetches for both of them can be executed independently, so there is no need to wait for one of them to complete before starting another, but how do we do that?

Zip

.zip() is an operation that combines items from two Observables into one by taking an item from each of the Observables and then producing a new value. It means that it waits for both of the Observables to produce a value before the flow continues.

This is useful in occasions where two values are produced independently but later (downstream), they are both consumed at the same time.

Let's see an example of that:

Observable.zip(
        Observable.just("One", "Two", "Three"),
        Observable.interval(1, TimeUnit.SECONDS),
        (number, interval) -> number + "-" + interval
)...