AndroIdiots Podcast 11: Functional & Reactive Programming
Yes, Functional & Reactive Programming isn’t for the faint heart. There, I said it. This is the first question that pops into mind as soon as we see the words “Reactive Programming”. But then what is the point of learning it. This is what we are going to explore in our new episode with Ashish Krishnan & Gurpreet Singh from Kite & Amanjeet Singh from 1mg.
Why is Functional & Reactive difficult?
These are difficult because of the way we have been taught programming from our childhood. Object-Oriented & Imperative Paradigms have been dominating from a few decades but times are changing. CPUs have becomes multi-core instead of getting faster and Memory has become cheap. Systems are not synchronous anymore but fast, responsive and async. We need an easy way to deal with all the asynchrony in the world and this is where ReactiveX (Reactive Extensions) have helped us.
RxJava/RxKotlin — The reactive libraries.
ReactiveX have made it very easy to write reactive code in most of our imperative languages by providing a set of tools which work on streams of data whether synchronous or asynchronous. RxJava is the API for Asynchronous programming with observable streams for Java language. RxKotlin enhances usage of RxJava API in a beautiful Kotlinish way but works with RxJava.
What is the fundamental of ReactiveX?
Lego Blocks Of RxJava:
Observables: Data streams that we hook into. These emits data/events which we listen to and operate upon. Remember subscribing to articles or newsletters online. Yeah those are our observables.
Operators: This is where Rx borrows stuff from Functional world. Operators are functions, mostly pure, which allow operating on data emitted by Observables.
map are just some basic examples. There a lot more which make Rx really fun. Ever used a reader view in your browser to get rid of all the noise in an article and just focus on content. That’s an operator.
Observer: These consume the data and are on the consumer end of the Data streams. Consuming your subscribed article makes you an observer. Someday your subscription will end or you will terminate it, till then the stream lives and keep pushing data down the pipeline.
Schedulers: Rx has given us a very easy to add thread management to Observables. With just two lines of code we can subscribe to a data stream and perform heavy operations on it on another thread, and observe the data back on our main thread.
How does Rx help in Android?
Android is completely asynchronous. Touch events, Activity Life-cycle, Network Operations, Sensor updates everything is async and provides a callback. So how do we compose so many different types of callbacks into a single, composable & simple understandable form. We could make everything Observable because everything is essentially an Observable in Android. It saves us from a ton of Callback Hell.
And above all, we have a single main thread available to us for all operations. Easy asynchronous programming with Rx helps us switch between background thread to main thread and keep our UI jank free.
RxAndroid provides Android specific bindings for RxJava that make writing reactive components in Android applications easy and hassle-free.
What else does Rx have to offer?
There is a lot more to Rx like BackPressure handling, concept of Subjects, Observables vs Flowables vs Singles vs Maybe etc. We do discuss these a bit in our podcast episode but these concepts are a bit advanced and should be learnt slowly.
How do I start?
Our guests and your hosts have prepared a very nice list of articles and websites to start learning Rx today.
1. What is Reactive Programming?
2. Reactive Programming for Android
3. Wrapping your brain around F&RP
4. Introduction to Reactive World
5. Be Reactive Series. Part-I, Part-II, Part-III
6. Interactive Marbles diagrams to understand Reactive Operators
7. Mind blowing Rx Operators via Animations.
8. An effort by Amanjeet Singh to show Rx Operators via Animations on an Android App called RxAnime. Feel free to send pull requests.
Book you can refer:
1. Reactive Programming in Java
We are inviting Android Developers to contribute back to the community via AndroIDIOTS. If you are interested in recording a podcast or writing tech articles, we would love to have you on board. Here is a small form you can fill so we can get back to you: