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

-ify Merge -os

-os First -um

-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”

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--

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 :)

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 :)