Fueled Engineering
Published in

Fueled Engineering

`Rxify` — a simple spell for complex RxJava operators (Part -2)

New to the `Rxify` spell? Checkout this post first. In this post we will learn about how we can hit cache first before making a server call. We will also learn some extremely helpful spells like :

  • Concat-ify
  • Merge-os
  • First-um
  • Take-o

In order to learn about these spells, we first need to read some literature. Let us go ahead and dive deep into the marble diagrams and some boring theory before we apparate (transport) right in the middle of Professor Snape’s lecture.

Concat-ify (concat() operator)

“emit the emissions from two or more Observables without interleaving them”

source

Merge-os (merge() operator)

“combine multiple Observables into one by merging their emissions”

via-Hedwig : difference between merge() operator and concat() is that merge can interleave the observables whereas concat never interleaves them.

Okay, enough of theory let’s apply this first

Snape’s Assignment

Professor Snape instructed all his students to write an essay on werewolves. The students who will turn in the essay first will get more points than the ones submitting later. Students are divided into four house observables (GryffindorObservable, SlytherinObservable, HufflepuffObservable and RavenclawObservable). Draco Malfoy wants his house (Slytherin) to get maximum points in this assignment and thus decides put a spell on the AssignmentEvaluator.

------G1--G2---G3---G4---------------------
-------------R1--R2----R3-----R4-----------
-------------H1-----H2----H3----H4---------
---------S1----S2------S3------------S4----

Draco casts a Concat-ify spell on the AssignmentEvaluator : Concat-ify (slytherin, hufflepuff, ravenclaw, gryffindor).

--S1--S2--S3--S4--H1--H2--H3--H4--R1--R2--R3--R4--G1--G2--G3--G4--
source

Hermione is not so happy about this. She needs to make things right. So, being the clever witch she is, she decides to replace the Concat-ify spell by another spell : Merge-os

------G1--G2---G3---G4---------------------
-------------R1--R2----R3-----R4-----------
-------------H1-----H2----H3----H4---------
---------S1----S2------S3------------S4----

Hermione casts a Merge-os spell on the AssignmentEvaluator.

--G1--S1--G2--R1--H1--G3--S2--R2--G4--H2--R3--S3--H3--R4--H4--S4--

Everything is back in order. Problem Solved :)

source

Cache-First-Then-Network

Let us now apply the spells that we have learnt to our daily tasks. It is a good practice of checking if the data is available in the cache and making a network request only if data is not available. This task can be easily achieved by using the concat-ify spell.

In the above case, if we will find the List<Lecture> in cache then the task of fetching the List from server will not run. We have also used the take-o spell here as we only want one result to be emitted. We can also use the first-um spell instead. Fore more details you can refer this post by Dan Lew.

via-Hedwig : The difference between the two calls is that first() will throw a NoSuchElementException if none of the sources emits valid data, whereas takeFirst() will simply complete without exception. — Dan Lew

via-Hedwig : Observable.concat() and observable.concatWith() operators are essentially doing the same thing. Difference is just that one is an instance method while other one is a static method.

Checkout Part 3 for getting an insight into the Battle of the Department of Mysteries and learning more about the term backpressure.

Full code coming soon on github :) Thank You for reading. My proposed talk on `Rxify-ing` our apps in DroidconIN 2016 has been accepted, stay tuned for the video which will be uploaded after the conference (10th & 11th November, 2016).

Mischief Managed :)

--

--

--

Publishing insights and discoveries on iOS and Android app development since 1982.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Garima Jain

Garima Jain

Android @GoDaddy Studio

More from Medium

Options Menu In android

Dealing with Multiple APIs via Retrofit? Learn How to use Sealed Classes Effectively

Handling project dependencies using type-safe project accessor

KMM — Publish Android / JVM SDK Binaries