The following is the first few sections of a chapter from Android's Architecture Components, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


LiveData Transformations

Sometimes, the data that you want is not the data that you get:

The LiveData system has some limited support for “transformations”, which help you adapt an existing LiveData into one that changes the data to better suit your needs. You can also create your own transformations, if desired. In this chapter, we will explore all of this.

The Bucket Brigade

LiveData is designed to be a simplified form of a reactive framework like RxJava.

Anyone who has looked at RxJava code knows that it has a tendency towards long chains of calls, to configure a stream of data, and sometimes to modify that stream along the way.

For example, you will find code like:

Observable<String> observable=Observable
  .create(new WordSource(getActivity()))
  .subscribeOn(Schedulers.io())
  .map(s -> (s.toUpperCase()))
  .observeOn(AndroidSchedulers.mainThread())
  .doOnComplete(() -> {
    Toast.makeText(getActivity(), R.string.done, Toast.LENGTH_SHORT)
      .show();
  });

Here, we:

(if you are interested in learning more about RxJava, see The Busy Coder’s Guide to Android Development)

In particular, map() is a transformation “operator”, in Rx terms. map() takes an object from our stream of data (in this case, a word) and transforms it into something else, which flows downstream to the subsequent chained calls. In this case, map() transforms a String into a String, where the “transformation” is converting the input String to uppercase to use as the output String.

RxJava has a dozens of such operators. In contrast, LiveData has two, and we will implement a third ourselves to see how that is accomplished.

Mapping Data to Data

The preview of this section apparently resembled a Pokémon.

Mapping Data to… LiveData?

The preview of this section was stepped on by Godzilla.

Writing a Transformation

The preview of this section left for Hollywood to appear in a reality TV show.

Do We Really Want This?

The preview of this section was eaten by a grue.