Party tricks with RxJava, RxAndroid & Retrolambda
“Users expect real time data. They want their tweets now. Their order confirmed now”. As a developer, you demand fire-and-forget messaging. You really don’t want to be blocked waiting for a result… You want to have the result pushed to you when it is ready, whenever that is. Even better, when working with big result sets, you want to receive individual results as they are ready because you don’t want to wait for the entire set to be processed before you see the first item on that list. Developers have lots of tools to push data, that’s easy. Developers need tools to react to pushed data.
Let there be light… even better… Reactive Extensions!
Described as “a library for composing asynchronous and event-based programs by using observable sequences” it is very well suited for introducing and managing concurrency for the purpose of offloading. That is, performing a given set of work concurrently to free up the current thread (usually the UI thread). A very popular use of this is to maintain a responsive UI, which is essential for a great mobile user experience!
I’ve been assembling a few usage examples as I learn how to use them in the last couple of months and I’ve decided to share them…
The basic building blocks of reactive code are Observables and Subscribers. An Observable emits items; a Subscriber consumes those items.
Import your dependencies:
…and configure retrolambda:
UPDATE: I’ve written a small tutorial about a super easy way to configure retrolambda https://medium.com/@cesarmcferreira/retrolambda-on-android-191cc8151f85
Fire up your IDE and let’s try’em out!
Some people assume that, because RxJava is all about “asynchronous sequences”, everything will happen on different threads by default, but that’s not the case so don’t forget to define both .observeOn() and .subscribeOn(). The first defines the thread in which the Observer will be subscribed (thread where the data will be received upon completion) and the latter produces a new Observable which will cause your subscription to occur on a thread retrieved from the specified scheduler instead of the thread from which subscribe() is called (android’s main thread, the UI thread).
… + RxJava + RxAndroid + Retrolambda
Let’s see them all in action:
Pretty sweet right?
Here’s a few extra links with cool information:
- RxJava official documentaion https://github.com/ReactiveX/RxJava/wiki;
- Great article series by Dan Lew on his blog http://blog.danlew.net/;
- Jake Wharton on Retrofit and RxJava (Netflix Open Source Meetup S02E02 07/2014) https://speakerdeck.com/jakewharton/2014-1;
- Kaushik Gopal (co-host of the great fragmented podcast) provides primers on RxJava and RxAndroid https://www.youtube.com/watch?v=k3D0cWyNno4;
- Awesome list of examples by Kaushik Gopal https://github.com/kaushikgopal/RxJava-Android-Samples;
- Jake Wharton on Testing, SqlBrite, NotRxAndroid, RxJava and Much More http://fragmentedpodcast.com/episodes/7/;
- Retrofit by Square http://square.github.io/retrofit/
- RxJava https://github.com/ReactiveX/RxJava;
- RxAndroid https://github.com/ReactiveX/RxAndroid.
If you have suggestions you want to see added ask away or create a pull request to this gist https://gist.github.com/cesarferreira/510aa2456dc0879f559f
Hit me up on @cesarmcferreira if you have any questions☺
Published in Startups, Wanderlust, and Life Hacking