Reactive programming on Android with RxJava

android

Reactive programming on Android with RxJava, Java8 or any other language is a programming style that is unlike others styles of programming namely transformational programming, interactive programming etc. and works on the flow of data i.e. data resourcing and data consumption. The behavior of the program is changed according to the environment, usually known as the event-driven approach.

This nature makes programs written in reactive programming ‘reactive’, i.e. responsive from the user perspective.

There are basically three factors; the source of data, consumer of data and connecting source to consumer.

Let’s consider an example:

Suppose there is an ATM, used to withdraw cash, in this scenario the machine is the source of data (cash) and you are the consumer of data. The machine reacts when the card and pin code is inserted.

Now, it has to present either of the two behaviors:

  1. If the amount of money requested is available it dispenses the cash and shows a thank you message.
  2. If the amount is not available in the account, it shows a failure message.

This event-driven approach if coded in reactive programming method using any libraries, in this case, we are using Rx Java for reactive programming, the code seems like this:

// defining the source of data

Observable<Integer> source = Observable.range(1, 5);

// defining the consumer of data

Subscriber<Integer> consumer = new Subscriber<Integer>() {

   @Override

   public void onNext(Integer number) { System.out.println(number); }

   @Override

   public void onError(Throwable e) { System.out.println(“Transaction failed, not enough balance”); }

   @Override

   public void onCompleted() { System.out.println(“Thankyou For the transaction”); }

};

// and finally connecting the consumer to source

source.subscribe(consumer);

Source of Data

Like in the example of ATM, the Source of data is the machine and other transactional details.

Similarly, the Observable<T> is the source in the above code. That is able to give output within a range of 1 to 5 and then finish. There are several methods to create the Observable the one used here is Observable<range>(int start, int count).

Consumer of Data

Subscriber<T> represents the Consumer of data. Like we are using Android with RxJava, it pushes the data Source (Observable) emits to the Consumer (Subscriber), by using the command onNext(T, data). The example code we mentioned will print the received numbers that can be referred as to dispensing the cash from the machine.

RxJava will signal the completion message on the subscriber, once all of the data is emitted by the Source, using onComplete() method.

In the case of an alternative condition that is the absence of asked amount of cash (absence of data on the Source), this program handles it by forwarding an error onError(Throwable e) on the subscriber.

In the example above that Consumer handles the exception by displaying an error message. While in the ATM the transaction failed’ error is shown on the screen.

And Finally, connecting the consumer to the source

This connection is established by using the Subscribe command on the Observable like subscribe(Subscriber s). Once the connection is made, in RxJava, only then all the computations and functionalities are run given on the Source and the Consumer.

Closing Words: Reactive Programming on Android with RxJAva

So, in this blog we described the basics of reactive programming on Android with Rx Java, where we define a Source of data, a consumer and then make connection by the Subscribe command on Observable, the Source emits data to the Consumer, data is pushed using on onNext(T data) method from Source to Consumer, on completion the signal onComplete() is triggered, on error the signal onError(Throwable e) is given and as per the code, conditions and computations exceptions are handled.

Here is a very good repository to start off with reactive programming with RxJava presented by DZone.

Hope you enjoyed this introductory learning of reactive programming, and now you are able to start off. Stay tuned for more!

Great courses are available online at Udemy to learn Android with Rx Java in the supervision of expert trainers and reliable resources.

Do you want 40% off coupon?
$30 isn’t more worthy, When You Get to Learn to automate your Systems entirely.
• Progressive Startups tend to implement completely automated DevOps pipelines from the get go.
• Build highly sophisticated automated build pipelines instantly with TimeCity, a feature-rich toolset
• Developers and DevOps Engineers, Tech Founders and Co-Founders (Technical or Non-Technical), Mobile and Web App Developers, IT Administrators and Software Architects need what TeamCity offers!


Do you want 30% off coupon?
Learn CI (Continuous Integration) with Jenkins, with a golden and limited time offer of $20!
• Jenkins is an award-winning open source toolset
• Reduce overall cost to develop Innovative Software with Jenkins toolset.
• Demands of CI and DevOps skilled developers are steadily increasing and eventually compensation rates too!


Do you want 50% off coupon?
Get ready to own you very Private Cloud Infrastructure. And now in just $50!
• Learn OpenStack from scratch to the depth.
• Build and Manage your own IaaS Cloud Infrastructure
• Cloud engineers, Software Engineers, IT Administrators, System designers, Software and IT architects, technology leader looking to utilize cloud computing in their organization. All of you are welcome.


Do you want 50% off coupon?
Get ready to own you very Private Cloud Infrastructure. And now in just $50!
• Learn OpenStack from scratch to the depth.
• Build and Manage your own IaaS Cloud Infrastructure
• Cloud engineers, Software Engineers, IT Administrators, System designers, Software and IT architects, technology leader looking to utilize cloud computing in their organization. All of you are welcome.


Do you want 40% off coupon?
$30 isn’t more worthy, When You Get to Learn to automate your Systems entirely.
• Progressive Startups tend to implement completely automated DevOps pipelines from the get go.
• Build highly sophisticated automated build pipelines instantly with TimeCity, a feature-rich toolset
• Developers and DevOps Engineers, Tech Founders and Co-Founders (Technical or Non-Technical), Mobile and Web App Developers, IT Administrators and Software Architects need what TeamCity offers!