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 this LiveData 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)Content copied to clipboard
Whenever either this or one of the lists inside of this changes, the returned LiveData value is also updated.