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

Garima Jain
Fueled Engineering
Published in
3 min readAug 10, 2016

--

This post is about the operators which upon reading the document look daunting at first but if you get a grasp of them, they can be quite useful in almost all the apps you code. RxJava is your friend, it will make your life easier. With RxJava many complex tasks can be accomplished easily and without errors. As the title says, `Rxify` - you just need to cast the spells and all your work will be done. From hitting the cache-first to performing retries, just cast the spells and you are good to go. You can jump directly to Part 2.

Spells

Spells you will learn in this tutorial :

  • Concat-ify
  • Debounce-y
  • Defer-ous
  • Delay-rium
  • Take-o
  • Repeat-ium
  • Zip-pyosa
  • Skip-y
  • Join-ikulus
  • Filter-rum
  • Retry-ikulus
  • Buffer-undum
  • Chunkify
  • Misc

Zip-pyosa (zip() operator)

“combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function”

source

Hermione is caught up in a difficult scenario where-in she wants to prepare poly-juice portion (HerTask). She is waiting for Ron(RonTask) to bring FluxWeed and Harry (HarryTask) to bring hair. Both RonTask and HarryTask are executing asynchronously. Only after both RonTask and HarryTask are completed then HerTask can start execution.

Without RxJava — use boolean flags (a BIG NO)

Hermione is so clever she decides to cast the `zip-pyosa()` spell instead.

source

Its Zip-pyOsa not Zip-pyosAR

via-Hedwig* : advantages of using a zip() operator are not just limited to combining two asynchronous operations, there’s more to it.

During the Triwizard tournament, Professor McGonagall is organising the Yule Ball dance in which she needs order. All the triwizard champions must accompany their partners to the dance floor in order. At one end all the champions (C) are entering and from other end of the hall their partners (P) are entering.

—--C1—--C2---C3---C4--- (Champions)---P1---P2---P3---P4--- (Partners)

McGonagall asks Ron to step up and solve this problem. Ron thinks of using `combineLatest()` spell to solve this problem. All is well, but the champions and their partners decide to enter the floor as follows :

-C1-------C2-—-----C3-----C4----P1--P2------P3-—---P4--—-- 

and Ron ends up creating a fool of himself.

Using combineLatest() spell

<C1,P1> <C1,P2> <C2,P2> <C2,P3> <C3,P3><C3,P4><C4,P4>
Source

Guess who then teaches Ron the correct way to do it?

via-Hedwig* : When you need a signal that sends a value each time any of its inputs change, use +combineLatest:. When you need a signal that sends a value only when all of its inputs change, use +zipPatrick Bacon

-C1-------C2-—-----C3-----C4----P1--P2------P3-—---P4--—--

Using zip-yosa() spell

<C1,P1> <C2,P2> <C3,P3> <C4,P4>

Professor McGonagall is really happy :)

via-Hedwig* : `observable.zipWith()` and Observable.zip() are essentially doing the same thing, one is a member function and other one is a static class method.

Stay tuned for more adventures of Ron, Hermione and Harry and learn how to `Rxify` day-to-day tasks.

*via-Hedwig : Meaningful messages from our friends far away. (Owl’s Delivery)

Checkout Part 2 to see Professor Snape teach the class of Defence Against the Dark Arts.

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!

--

--