Intro to RxJava

sourabh gupta
5 min readJul 16, 2020
Rxjava – AndroidPub

What is RxJava?

RxJava is a Java VM implementation of ReactiveX .It is library for composing asynchronous and event-based programs by using observable sequences.

Why RxJava?

  1. Remove Boilerplate code
  2. RxJava makes multi-threading super easy. Using imperative approach, moving a piece of code to background thread is hard work. However in RxJava, we could easily define what thread each part of the chain would be in.
  3. RxJava is composable, Rx operators can be combined to produce more complicated operations.
  4. Rx operators can transform one type of data to another, reducing, mapping or expanding streams as needed.
  5. Error handling becomes much easier with RxJava. You don’t need to worry about adding try catch blocks everywhere.
  6. The RxJava library was designed to give a control over a wide range of data, simultaneously on the events in the real-time. This allows us to build highly responsive mobile applications.

RxJava Vs RxAndroid

RxAndroid is just a layer on top of RxJava which provides android specific support.There is new Scheduler class (AndroidSchedulers.mainThread()) in RxAndroid.

If you study any Rx Java implemented code, all of the time, you will find three main constructs

  1. Observables
  2. Schedulers
  3. Observers

Observables

Observables is part of code which is used to emit and process data. Many Subscribers(Observers) can subscribe to Single Observable.We can modify data emitted by observables by using Operators

Schedulers

We can achieve Multithreading in RxJava using Schedulers.A scheduler can be recognized as a thread pool managing one or more threads. Whenever a Scheduler needs to execute a task, it will take a thread from its pool and run the task in that thread.RxJava works on single thread by default.We can specify thread on which Observable emit data and on which Observer observe data.

Different types of Schedulers available in RxJava

Schedulers.io() :

This can have a limitless thread pool. Used for non CPU intensive tasks. Such as database interactions, performing network communications and interactions with the file system.

Schedulers.io() :

This can have a limitless thread pool. Used for CPU intensive tasks. Such as image processing.

AndroidSchedulers.mainThread()

This is the main thread or the UI thread. This is where user interactions happen

Schedulers.newThread()

This scheduler creates a new thread for each unit of work scheduled.

Schedulers.single()

This scheduler has a single thread executing tasks one after another following the given order.

Schedulers.trampoline()

This scheduler executes tasks following first in, first out basics.We use this when implementing recurring tasks.

Schedulers.from(Executor executor)

This creates and returns a custom scheduler backed by the specified executor.

Observers

Observers is the one who receives data emitted by Observables.

One Observable can have many observers. An observable emit data, if there is at least one observer subscribed for the data. If there is no subscription observable will not emit data.

Main observer methods

  • OnSubscribe():-Each time when observer subscribe to Observable ,OnSubscribe() Method called.
  • onNext() :- Each time an Observable emits data it calls to Observer’s onNext() method passing that data.
  • onError() :- If any error occurs Observable calls to Observer’s onError() method.
  • onComplete() :- Observable invokes Observer’s onComplete() method, when the data emission is over.

What is Operator?

Operator is used to modify and filter data emitted by Observables.if we apply multiple operator then it forms a chain ,Every Operator operate on data emitted by previous operator.Order is important in case of chain,

There are many operators in RxJava,Some of are given below.

  1. Just Operator-The Just operator converts an item into an Observable that emits that item.
  2. From Array Operator-it creates an Observable from set of items using an Iterable, which means each item is emitted one at a time.
  3. Range Operator-Range() creates an Observable from a sequence of generated integers. The function generates sequence of integers by taking starting number and length. So the above same examples can be modified as Observable.range(1, 10).
  4. Create Operator-Creates an Observable from scratch and allows observer method to call programmatically.

5. Map Operator- Transform the items emitted by an Observable by applying a function to each item

6. Flat Map Operator-transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable

7. Concat Map Operator-ConcatMap works almost the same as flatMap, but preserves the order of items. But concatMap has one big flaw: it waits for each observable to finish all the work until next one is processed

8. Buffer Operator-The Buffer operator transforms an Observable that emits items into an Observable that emits buffered collections of those items

9. Filter operator-The Filter operator filters an Observable by only allowing items through that pass a test that you specify in the form of a predicate function.

10. Distinct Operator-The Distinct operator filters an Observable by only allowing items through that have not already been emitted.

11. Skip Operator-You can ignore the first n items emitted by an Observable and attend only to those items that come after, by modifying the Observable with the Skip operator

12. Skip Last-You can ignore the final n items emitted by an Observable and attend only to those items that come before them, by modifying the Observable with the SkipLast operator.

Summing-up

I think this articles will help you to understand importance of RxJava.
This article surely motivate to you to refractor your old code to handling code to background tasks.Start using it from now.More you practice,More you learn.

Keep Exploring and Keep Learning…………

--

--