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

Garima Jain
Fueled Engineering
Published in
4 min readAug 21, 2016

If you are new to the `Rxify` spell then you should first have a look at Part1 and Part2. In this post we will learn about the term `backpressure` and some of the spells which can be used to control it. We will also look at some interesting problems which can be solved using these spells.

Backpressure

strategies for coping with Observables that produce items more rapidly than their observers consume them — Backpressure Operators

It simply means that the frequency of Producer producing an item is more than the ability of Consumer to consume. This situation can be tackled or controlled using various techniques and here we will learn some spells which can make our lives easier. Let us consider a scenario first :

Battle of the Department of Mysteries

Dumbledore’s army (Harry, Hermione, Ron and others) is fighting against the Death Eaters (Lucius Malfoy, Bellatrix Lestrange and others). Each team is casting spells against the other team to gain possession of the prophecy (a shiny, smokey-white, gem-like ball) . Dumbledore’s army is not as experienced as the Death Eaters and in order to cope up with the overwhelming attacks of death eaters something should be done.

Buffer-undum (buffer() operator)

periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time

source

Harry decides to set up a Buffer-undum spell to consume the produced spells by the Death Eaters.

public Observable<List<Spell>> bufferundum() {
return getDeathEaterSpellsObservable()
.buffer(3); //count
}

This gives Dumbledore’s army some time to consume the incoming spells but also results in a lot of spells getting piled up and the Dumbledore’s army falls behind. This was a lossless way of dealing with backpressure but here what we really need is a lossy way instead.

source

Debounce-y (debounce() operator)

only emit an item from an Observable if a particular timespan has passed without it emitting another item

source

This is when Sirius Black comes to the rescue. He decides to set up a debounce-y spell instead which will filter the incoming spells such that : All the spell attacks except the last one that happened within the time window of 300ms will get dropped.

Sirius casts a debounce-y spell which results in the victory of Dumbledore’s army as the number of attacks is reduced.

public Observable<Spell> debouncey() {
return getDeathEaterSpellsObservable()
.debounce(300, TimeUnit.MILLISECONDS);
}
source

via-Hedwig : The terms lossy and lossless have been inspired from the post here.

Optimize network traffic

Harry’s skills which he learnt at Hogwarts are not wasted. Let us consider a scenario where lossless backpressure handling is required. We need to track user events using any analytics library. Naive way of performing this task is to place a server request whenever an event occurs. As network resources are limited, here we will run into the case over chatty producer (user events) and a slow consumer (network request). Let’s apply here buffer-undum spell instead.

By using the buffer-undum spell above we are buffering three Events and sending them together in a single request.

via-Hedwig : Before applying a buffer operator in such a scenario, you must first confirm that the analytics API supports sending multiple events in a single request.

Auto-Search

When user is typing into a search EditText we want to display the results on the screen. Basic implementation is to place a network request whenever any character changes. Again, this is an example of backpressure where the overchatty producer (user input) is overwhelming the consumer (network request). If he user wants to type India, we are okay to lost a few search requests here : (I, In, Ind, Indi). The only one we want is the final search term India when the user has stopped typing. Let’s apply our lossy debounce-y spell here. Great example here.

via-Hedwig : The solutions mentioned in this post are not the only ones and one can apply other operators too which can achieve similar or maybe even better results.

via-Hedwig : Also see DebouncedBuffer.

If you want to learn about the spells like Repeat-ium, Retry-kulus and Filter-rum form Professor Lupin, then Go to the next class here.

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

--

--