Chapter 2: Observer

android2ee (Seguy Mathias)
1 min readDec 2, 2020

Creating an Observer to listen for the data stream of observable. Let’s suppose we have the observable from chapter one:

We would like to know the number of days in a week emitted, so we need to be sure the emission is finished. And we will test it like this:

The goal is to create the observer

subscribe() with an observer instance

So let’s create an instance of an observer

subscribe() with lambdas

Your just need to pass directly the lambda function to the method subscribe as its parameters. It’s the usual way.

The three lambda ways

You can omit onError (but you shouldn’t) and onComplete (if you want).
Let’s have a remark before keep going. Lambda can be passed in different ways:

More on the topic

Previous chapter: Chapter 1: Observable

Next chapter:

This chapter is a small part of a biggest training project, you can find on Github. You’ll have the main branch with the answers/responses/unit tests and documentation. You could start with the “questions” branch to only have the questions.

You’ll have the full table of content of the articles on Medium.

You can download the book on Android2ee

Warning: Those articles has been created only because I am stuck in a Rx Android project and I need to prepare the ramp up for those who will replace me.

My conclusion : Choose Kotlin and suspending function instead of Rx. PLEASE, save your project from RxHell !!!

Write Medium in Markdown? Try Markdium!

--

--

android2ee (Seguy Mathias)

Android2ee alias Mathias Seguy. Android expert gives you some news on the Android world. mathias.seguy@android2ee.com