combineMap

fun <S, T> combineMap(vararg sources: LiveData<out List<S>>, combineMapFunction: (S) -> T): LiveData<List<T>>

Transforms multiple LiveDatas containing lists of type S, into one LiveData with a single list of type T. combineMapFunction is used to map each of the sources' items into a list.

Consistent with kotlinx.coroutines.flow.combine, combineMapFunction will only be called when all provided sources have a value set (even if that value is null). Until then, the returned LiveData's value will be unset and observers will not receive callbacks.

Like MediatorLiveData instances, LiveData.getValue returns null as long as the LiveData instance is not observed. When the returned LiveData instance gets an active observer the combineMapFunction is called. While the LiveData instance has an active observer the combineMapFunction is called for each value set to one of the sources.

For example:

// GIVEN
val source1 = Holder(1, 2, 3)
val source2 = Holder(4, 5)

// WHEN
val combineMapped = combineMap(source1, source2) { holder -> holder.contents }

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

Whenever one of the lists in sources changes, the returned LiveData value is updated.

Parameters

S

The type of the elements in all source lists.

T

The type of the list elements transformed by mapping.

combineMapFunction

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