Why you should learn Reactive Programming

Ovidiu Latcu
Dec 29, 2017 · 6 min read

Quick intro

So what is reactive programming ?


The building blocks of Reactive Programming

Observables (& Flowables in RxJava2)

  • Observables are the data source /stream
  • can emit multiple values, just one, or none
  • can also emit errors
  • can be infinite or finite, in which case they emit their completion event

Subscribers

  • Subscribers subscribe to Observables
  • they consume/observe the data
  • they also receive the errors and completion events from the Observable

Operators

  • used to create Observables (timers, ranges, from other data sources)
  • used to transform Observables (map, buffer, group, scan, etc)
  • used to filter Observables (filter, distinct, skip, debounce, etc)
  • used to combine Observables (zip, merge, combine latest, etc)

Schedulers

  • mechanism that allows us to easily add threading to our Observables and Subscribers
  • subscribeOn() specifies on which Scheduler to Observables should operate
  • observeOn() specifies on which Scheduler/Thread the Subscribers should be notified

Advantages of Reactive Programming

Still not sure why you should start learning RX ? Here’s a short list of advantages :

  • a lot simpler to do async / threaded work
  • a lot of operators that simplify work
  • very simple to compose streams of data
  • complex threading becomes very easy
  • you end up with a more cleaner, readable code base
  • easy to implement back-pressure

Concrete example

I know RX is hard to understand from the first try, but I think taking a concrete example will make this easier.

  • just take into consideration location when accuracy is higher than 20 meters
  • it should also track device acceleration and magnetic sensor from which it should compute the azimuth(orientation angle relative to North)
  • combine the latest values from each data ( location , azimuth )
  • at every 5 seconds submit the data to the API
  • do all this work on a background Thread
  • once a response is received from the API, post it on the UI Thread so we can display it to the user

Corebuild Software

Android, iOS, Web Development www.corebuild.eu

Ovidiu Latcu

Written by

Android Tehnical Lead @ Corebuild

Corebuild Software

Android, iOS, Web Development www.corebuild.eu