{"id":6294,"date":"2018-07-06T09:28:32","date_gmt":"2018-07-06T09:28:32","guid":{"rendered":"https:\/\/9series-blog.staging9.com\/?p=6294"},"modified":"2026-03-03T10:47:11","modified_gmt":"2026-03-03T10:47:11","slug":"introduction-rxjava-rxandroid","status":"publish","type":"post","link":"https:\/\/www.9series.com\/blog\/introduction-rxjava-rxandroid\/","title":{"rendered":"Introduction to RxJava and RxAndroid"},"content":{"rendered":"<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/Introduction-to-RxJava-and-RxAndroid-.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3217\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/Introduction-to-RxJava-and-RxAndroid-.jpg\" alt=\"Introduction to RxJava and RxAndroid 9series solutions\" width=\"800\" height=\"500\" \/><\/a><\/p>\n<p><strong>What is RxJava?<\/strong><br \/>\n\u201cRxJava 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.\u201d<\/p>\n<p>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.<\/p>\n<p><strong>What is Observable?<\/strong><br \/>\nObservable 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).<br \/>\nWe 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.<\/p>\n<p><strong>What is Schedulers?<\/strong><br \/>\nRxJava 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().<\/p>\n<p>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().<\/p>\n<p><strong>What is an Observer?<\/strong><br \/>\nAn 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().<\/p>\n<p><strong>What is Operator?<\/strong><br \/>\nAn 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.<\/p>\n<p>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.<\/p>\n<p><strong>What is RxAndroid?<\/strong><br \/>\nRxAndroid 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.<\/p>\n<p><strong>What is CompositeSubscription?<\/strong><br \/>\nCompositeSubscription 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.<\/p>\n<p><strong>How to Integrate in Android Project?<\/strong><br \/>\nFirst, you need to add the following dependency for RxJava and RxAndroid in your module level build.gradle file.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/12.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3218\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/12.jpg\" alt=\"Introduction to RxJava and RxAndroid - 9series Solutions\" width=\"800\" height=\"149\" \/><\/a><\/p>\n<p>Operators for creating Observable<br \/>\nWe can create Observable by using operators like Create, Just, From, Range, Interval etc.<\/p>\n<p><strong>Below is the example for creating observable.<\/strong><br \/>\n1. Create Operator:<br \/>\nYou can create observable from the scratch using create() operator.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/22.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3219\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/22.jpg\" alt=\"Introduction to RxJava and RxAndroid - code 9series\" width=\"800\" height=\"789\" \/><\/a><\/p>\n<p>2. Just Operator:<br \/>\nConvert and object of set of similar type objects into Observable.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3220\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/3.jpg\" alt=\"Introduction to RxJava and RxAndroid 9series solutions\" width=\"800\" height=\"15\" \/><\/a><\/p>\n<p>3. From Operator:<strong><br \/>\n<\/strong>Converts some other data structure into Observable.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3221\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/4.jpg\" alt=\"9series - Introduction to RxJava and RxAndroid\" width=\"800\" height=\"14\" \/><\/a><\/p>\n<p>4. Range Operator:<br \/>\nCreate Observable that emits sequential range of Integers.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3222\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/5.jpg\" alt=\"9spl solutions Introduction to RxJava and RxAndroid\" width=\"800\" height=\"17\" \/><\/a><\/p>\n<p>5. Interval Operator:<br \/>\nEmits 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.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3223\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/6.jpg\" alt=\"Introduction to RxJava &amp; RxAndroid\" width=\"800\" height=\"488\" \/><\/a><\/p>\n<p><strong>Operators for transforming Observable<\/strong><br \/>\nOperators that transform items that are emitted by Observable are Map, Buffer, FlatMap, GroupBy, Scan, Window.<\/p>\n<p>1. Map Operator<br \/>\nTransform an item emitted by an observable by applying function.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/7.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3224\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/7.jpg\" alt=\"Introduction to RxJava &amp; RxAndroid 9series solutions\" width=\"800\" height=\"635\" \/><\/a><\/p>\n<p>2. Buffer Operator<br \/>\nBuffer 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.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/8.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3225\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/8.jpg\" alt=\"9series solutions Introduction to RxJava &amp; RxAndroid\" width=\"800\" height=\"455\" \/><\/a><\/p>\n<p>3. FlatMap Operator:<br \/>\nTransform an item emitted by an Observable into Observables then flatten the emissions from those into Single Observable.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/9.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3226\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/07\/9.jpg\" alt=\"Introduction to RxJava - RxAndroid 9series solutions\" width=\"800\" height=\"575\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>We will learn more operators with an example in next blog post. For more operators visit http:\/\/reactivex.io\/documentation\/operators.html<\/p>\n<p><strong>Final Notes<\/strong><br \/>\n<strong>9series<\/strong> is a <a href=\"https:\/\/www.9series.com\/services\/android-application-development.html\" target=\"_blank\">top Android app development company<\/a> that has years of experience in delivering the best. Our developers have in-depth knowledge in rxjava for android app development.<br \/>\nStay tuned for our next Blog with more examples.<\/p>\n<p><strong>Source<\/strong><br \/>\nhttps:\/\/github.com\/ReactiveX\/RxJava<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is RxJava? \u201cRxJava 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&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":6295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[1427],"tags":[2242,2243],"class_list":["post-6294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android-app-development-company","tag-rxjava-for-android-app-development"],"_links":{"self":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/comments?post=6294"}],"version-history":[{"count":1,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6294\/revisions"}],"predecessor-version":[{"id":6305,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6294\/revisions\/6305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media\/6295"}],"wp:attachment":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media?parent=6294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/categories?post=6294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/tags?post=6294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}