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.
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.
2. Just Operator:
Convert and object of set of similar type objects into Observable.
3. From Operator:
Converts some other data structure into Observable.
4. Range Operator:
Create Observable that emits sequential range of Integers.
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.
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.
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.
3. FlatMap Operator:
Transform an item emitted by an Observable into Observables then flatten the emissions from those into Single Observable.
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