`Rxify` — a simple spell for complex RxJava operators (Part -4)
If you are new to the `Rxify` spell then you should first have a look at Part1, Part2 and Part3. In this post we will learn about the Repeat-ium spell, Retry-kulus (pronounced as ‘ritri’culous) spell and their magical effect. We will also look at the perks of learning spells like Filter-rum and Concat-ify.
Repeat-ium (repeat() operator)
“create an Observable that emits a particular item multiple times”
Simple Right? Okay! then let’s see the next one.
Retry-kulus (retry() operator)
“if a source Observable emits an error, resubscribe to it in the hopes that it will complete without error”
There’s an important difference between the above two spells :
via-Hedwig : repeat() resubscribes when it receives onCompleted().
retry() resubscribes when it receives onError(). — Dan Lew
Professor Lupin’s Riddikulus Lessons
In the Defense Against the Dark Arts lecture of Professor Lupin, students are learning how to perform a “Riddikulus” charm (magical spell to turn a Boggart — your worst fear, into something funny).
Today it’s Draco’s turn to learn the spell. Spell will be successful when the Boggart’s laughter level crosses the FUNNY_THRESHOLD. Draco needs to retry the spell if it fails. Even if the spell is successful, in order to achieve perfection Draco will repeat this task until Boggart’s laughter level crosses the HILARIOUS_THRESHOLD.
via-Hedwig : “Something is funny when it makes you laugh, but it becomes hilarious when it is extremely funny” — source
- Boggart has 3 states : SCARY, FUNNY and HILARIOUS.
- When we put a riddikulus charm on a Boggart, it’s state can either remain the same (if it doesn’t cross the FUNNY_THRESHOLD), OR it can change into FUNNY (if it crosses FUNNY_THRESHOLD) OR it can change into HILARIOUS (if it crosses HILARIOUS_THRESHOLD).
- We need to retry until the Boggart is atleast FUNNY.
- We need to repeat until the Boggart becomes HILARIOUS.
Well, clearly Draco is not in the mood to learn. On the other hand Ron Weasley is able to complete the task after learning the retry-kulus and repeat-ium spells. Let’s have a look how : Find the Boggart.java class here.
Gosh! Ron is feeling so proud!
Filter-rum (filter() operator)
We often run into the problem where we want to retry an API call and we should only retry when we get an IOException (no internet). Dan Lew explains this in detail here. We can use retryWhen() operator in this case.
via-Hedwig : Kaushik Gopal has taken an approach of using counters here (RetryWithDelay).
via-Hedwig : An interesting approach for polling and retrying with delay by using a combination of retryWhen() and repeatWhen() here.
While the user is browsing your our app we should keep the session active. We have the following two options :
- Checking if the session is valid before performing any API call which needs session token of the user, for which we would have to place session.isValid() check before each API call.
- Renew the token repeatedly while the user is browsing the app.
Second option can be easily done using our Repeat-ium spell with a sligh modification (For more information on using repeatWhen() see this post from Dan Lew here). The following example makes use of another helpful spell Concat-ify (If you haven’t yet learned that spell, learn it now from Professor Snape here.)
“While the user is browsing our app, keep the session active. Get current session from SessionManager : If the session is valid, return it. If the session is invalid, make an API call to renew the session and repeatedly renew it.”
Let’s solve it now :) Wands at the ready!
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 :)