Reactive Programming in Android(RxJava) – Part 1

Reactive Programming in Android(RxJava) – Part 1

Yes, its reactive season going on. As every decade or so pattern of coding have a drift as in a particular coding pattern is fascinated and accepted coders to ease and make their coding efficient.

In this tutorial we shall understand Basic RxJava and the concept the of observers and the simplest ways to implement it. hopefully i come out with the advanced version of this in upcoming posts

So What is Reactive Programming or with respect to this post RxJava ?
RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

Lets copy and paste the following 2 words into our minds as this post revolves around them.

1)Observable
2)Subscriber

I would like to explain the 3 main states of observable pattern with an example.
So lets take a coin an toss it in the air . Obviously when you TOSS a coin it lands on the side HEAD or TAIL.

just remember this example as we continue with knowing what is an observable.

Observable : Observable emits data and completes successfully or fails.

observable emits only when it has a subscriber subscribed to it.

observable has methods to it.

1) onNext : observable emits the data – which is like tossing a coin example which i just mentioned.
onNext Method can be called as many times as the observable emits data to the subscriber.

2) onCompleted : successfully emitted. Assuming HEAD as successful side a onCompleted is like a tossed coin landing on HEAD. No errors occured.

3) onError : error occurred. According to our assumption , the coin had landed on TAIL side. Error has occured in this state.

TOSS = onNext
HEAD = onCompleted
TAIL = onError

The example is just so that you can relate to it.

Subscriber : Subscriber Observes the observable which it is subscribing to and this is where we do something with the observed data.

In short :
observable emits data -> subscriber observes the data and do something with it.

Lets take a look at how to create an observable.

[code]Observable observableOfString = Observable.create(
new Observable.OnSubscribe() {
@Override
public void call(Subscriber<? super String> s) {
s.onNext("i am a string from observable");
s.onCompleted();
}
}
);
);[/code]

This is a observable which passes a string to the onNext method of the subscriber.In this case s

Now we need a subscriber to subscribe to this observable, so lets create one.

[code]
new Subscriber() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(String s) {
Log.d("Log", s + ",being printed by the subscriber");
}
}
[/code]

the above subscriber observes for a string and Logs it.

for a subscriber to know this observable should it subscribe to , we need to do the following.

[code]
observableOfString.subscribe(new Subscriber() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(String s) {
Log.d("Log" , s + ",being printed by the subscriber");
}
});
[/code]

lets have subscriber object just to make the code look simpler.

[code]
Subscriber subscriber = new Subscriber() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(String s) {
Log.d("Log" , s + ",being printed by the subscriber");
}
}
[/code]

no our observable and the subscription looks as simple as this.

[code]
observableOfString.subscribe(subscriber);
[/code]

The string which is logged will look like this
“i am a string from observable,being printed by the subscriber”

the subscribe() method returns a subscription.

[code]
subscription subscription = observableOfString.subscribe(subscriber);
[/code]

now you can unsubscribe to the subscription when you want.
In case on android you call the following code in onDestroy().

[code]
subscription.unsubscribe();
[/code]

Doing this you unsubscribe the subscription when the activity is destroyed . This will prevent the app crash when your async methods gets the data but the activity is been destroyed.

Hope the Basic Observable and the Subscriber concept is clear.
I suggest to go through the documentation to have an overall idea.

Happy coding 😉