O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extracting consumers

A very similar method extraction approach can be applied to Consumer actions, as in the following:

.doOnError(error -> {    Toast.makeText(this, "We couldn't reach internet - falling back to     local data",            Toast.LENGTH_SHORT)            .show();})

Here, the error handling code can be extracted with a method:

showToastErrorNotification()

So, the line becomes this:

.doOnError(showToastErrorNotification())

Also, the method itself contains this:

@NonNullprivate Consumer<Throwable> showToastErrorNotification() {return error -> {        Toast.makeText(this, "We couldn't reach internet - falling         back to local data",                Toast.LENGTH_SHORT)                .show();    };}

This way, the actions (consumers) become more descriptive and take less space in the flow ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required