flatMap

fun <T, S> LiveData<out Collection<S>>.flatMap(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<T>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in thisLiveData list by flattening it.

For example:

// GIVEN
val source = MutableLiveData<List<Holder>>(
listOf(
Holder(1, 2, 3),
Holder(4, 5)
)
)

// WHEN
val flatMapped = sources.flatMap { holder -> holder.contents }

// THEN
// Observing flatMapped.value yields listOf(1, 2, 3, 4, 5)

Whenever either this or one of the lists inside of this changes, the returned LiveData value is also updated.

Parameters

T

The type of the list elements to transform by flattening.

S

The type of the source list elements, containing lists of type T.

mapFunction

A function that maps source lists to lists of type T.