9series
Android

Introduction to RxJava and RxAndroid

July 6, 2018

Introduction to RxJava and RxAndroid 9series solutions

What is RxJava?
“RxJava is a Java VM implementation of Reactive Extension. Basically it is the Library which helps you to compose asynchronous and event-based programs by using observable streams. In RxJava everything in a reactive data stream.”

Where Observable emits a stream of data when the subscriber(Observer) starts listening to it. Basic building blocks of RxJava are Observable and Observer. You can create asynchronous data stream on any thread. Which can be managed by Schedulers. In short RxJava = Observable + Schedulers + Observer.

What is Observable?
Observable is a stream of data. Observable process and emits data when any subscriber starts listening to it. Observable have one or many subscribers.Observable can emit any number of items (zero or more).
We can also transform emitted data from a stream by applying different Operators on Observable like map, flatmap, groupBy, debounce, filter, join etc. Operators process data and return an observable. So we can apply any number of Operators on Observable.

What is Schedulers?
RxJava is asynchronous programming. By default, RxJava is single threaded. Which means Observable notifies its Observer on which thread its subscribe() was called. So if you want to introduce multithreading into your Observable you can do by instructing Observable on which thread Observable emit data is and on which thread notify its Observer using following methods subscribeOn() and observeOn().

There are many Schedulers available in Rxjava on which Observable emits data are Schedulers.io(), Schedulers.newThread(), Schedulers.computation(), Schedulers.single(), Schedulers.immediate(), Schedulers.trampoline() and Schedulers.from() For android notify on ui thread by implementing observeOn() method with AndroidSchedulers.mainThread().

What is an Observer?
An observer is received data stream emitted by Observable. Observer subscribes Observable using method subscribe(). When Observable emits data then this method of Subscriber is called onNext(). When completed successfully then this method is called onComplete(). If during this emission any exception is thrown by Observable then this method is called onError().

What is Operator?
An operator is a function used to transform emitted data from Observable. Mostly the Operator is used on an Observable and return Observable. If more than one operator is applied then it will be in a chain one after another.

Order of the operator is matter. A chain of an operator does not operate independently on the original Observable that originating the chain but they operate in a sequence and the next Operator operates on a result of last operators Observable.

What is RxAndroid?
RxAndroid is a library which is specific to the Android platform. RxAndroid library adds few classes on top of RxJava that makes writing reactive components in Android applications easy and hassle-free. Specifically, Schedulers classes (AndroidSchedulers.mainThread()) introduced in RxAndroid.

What is CompositeSubscription?
CompositeSubscription is a group of subscription that is unsubscribed together. CompositeSubscription is useful in android to prevent memory leaks. When the activity of fragment is no longer visible to the user and destroyed at that time we can unsubscribe it.

How to Integrate in Android Project?
First, you need to add the following dependency for RxJava and RxAndroid in your module level build.gradle file.

Introduction to RxJava and RxAndroid - 9series Solutions

Operators for creating Observable
We can create Observable by using operators like Create, Just, From, Range, Interval etc.

Below is the example for creating observable.
1. Create Operator:
You can create observable from the scratch using create() operator.

Introduction to RxJava and RxAndroid - code 9series

2. Just Operator:
Convert and object of set of similar type objects into Observable.

Introduction to RxJava and RxAndroid 9series solutions

3. From Operator:
Converts some other data structure into Observable.

9series - Introduction to RxJava and RxAndroid

4. Range Operator:
Create Observable that emits sequential range of Integers.

9spl solutions Introduction to RxJava and RxAndroid

5. Interval Operator:
Emits a sequence of Integer spaced by particular time interval continuously so if your task is complete unsubscribe it. We can also use it for timer purpose in our app.

Introduction to RxJava & RxAndroid

Operators for transforming Observable
Operators that transform items that are emitted by Observable are Map, Buffer, FlatMap, GroupBy, Scan, Window.

1. Map Operator
Transform an item emitted by an observable by applying function.

Introduction to RxJava & RxAndroid 9series solutions

2. Buffer Operator
Buffer as the name suggest it periodically gather from an Observable and store into bundles and emit bundles rather than a single item. A buffer can take count as well as time as an argument.

9series solutions Introduction to RxJava & RxAndroid

3. FlatMap Operator:
Transform an item emitted by an Observable into Observables then flatten the emissions from those into Single Observable.

Introduction to RxJava - RxAndroid 9series solutions

In the above example, just operator returns single item 1, 2 and 3, then apply flatMap operator. The flatMap operator returns Observable so that for better understanding here we use range with buffer operator.

We will learn more operators with an example in next blog post. For more operators visit http://reactivex.io/documentation/operators.html

Final Notes
9series is a top Android app development company that has years of experience in delivering the best. Our developers have in-depth knowledge in rxjava for android app development.
Stay tuned for our next Blog with more examples.

Source
https://github.com/ReactiveX/RxJava

Read our Recent Articles

AI Powered Community Banking: Why Automation Is the New Relationship Moat in Modern Banking
Uncategorized

AI Powered Community Banking: Why Automation Is the New Relationship Moat in Modern Banking

What is AI Powered Community Banking?  AI powered community banking is the use of artificial intelligence, built on a...

10 min read • Mar 25, 2026 Read more
Deep Learning and Machine Learning: Understanding Their Synergistic Relationship in Modern AI
AI Solutions / Artificial Intelligence

Deep Learning and Machine Learning: Understanding Their Synergistic Relationship in Modern AI

Artificial Intelligence has evolved rapidly over the past decade, largely driven by breakthroughs in Machine Learning and Deep Learning. While...

3 min read • Apr 29, 2024 Read more
AI and IoT Integration: Powering the Next Generation of Smart Connected Environments
AI Solutions / Machine Learning

AI and IoT Integration: Powering the Next Generation of Smart Connected Environments

Introduction  The convergence of Artificial Intelligence and the Internet of Things marks a major milestone in digital transformation. As businesses...

4 min read • Apr 19, 2024 Read more

Let's Discuss Your Strategic Technology Initiatives

Partner with 9series to accelerate your digital transformation journey. Our enterprise architects are ready to design solutions tailored to your unique challenges.

Trusted by global partners

Nailbiter NUs Safaricom Intuify Solvit i-banq Fractal