`Rxify`— a simple spell for complex RxJava operators (Part -1)
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 you will learn in this tutorial :
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”
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.
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.
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 :
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>
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 +zip — Patrick Bacon
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).