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

Leaking with Observables

As it happens, it is quite easy to leak memory when using Observables. Usually, Observables have access to the Activity instance because, at some point, it has to update the UI with the results of the computation.

Now, consider the Observable that we used earlier:

Observable.interval(0, 5, TimeUnit.SECONDS)

This Observable never completes by itself and thus it never terminates. It keeps emitting values after it has been subscribed to. Also, consider this block, found downstream down the flow:

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

It maintains a strong reference to the Activity by the this reference. This means that ...

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