combine
Transforms two LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.
Consistent with kotlinx.coroutines.flow.combine, combineFunction 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.
For example:
// GIVEN
val sourceLiveData1 = MutableLiveData<Int?>(1)
val sourceLiveData2 = MutableLiveData<Int>(2)
// WHEN
val combined = combine(
sourceLiveData1,
sourceLiveData2
) { source1, source2 ->
source1 ?: source2
}
// THEN
// Observing combined.value yields 1
// WHEN
sourceLiveData1.value = null
// THEN
// Observing combined.value yields 2
Parameters
Transforms three LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.
Consistent with kotlinx.coroutines.flow.combine, combineFunction 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.
For example:
// GIVEN
val sourceLiveData1 = MutableLiveData<Int?>(1)
val sourceLiveData2 = MutableLiveData<Int>(2)
val sourceLiveData3 = MutableLiveData<Int>(3)
// WHEN
val combined = combine(
sourceLiveData1,
sourceLiveData2,
sourceLiveData3
) { source1, source2, source3 ->
source1 ?: source2
}
// THEN
// Observing combined.value yields 1
// WHEN
sourceLiveData1.value = null
// THEN
// Observing combined.value yields 2
Parameters
Transforms four LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.
Consistent with kotlinx.coroutines.flow.combine, combineFunction 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.
For example:
// GIVEN
val sourceLiveData1 = MutableLiveData<Int?>(1)
val sourceLiveData2 = MutableLiveData<Int>(2)
val sourceLiveData3 = MutableLiveData<Int>(3)
val sourceLiveData4 = MutableLiveData<Int>(4)
// WHEN
val combined = combine(
sourceLiveData1,
sourceLiveData2,
sourceLiveData3,
sourceLiveData4
) { source1, source2, source3, source4 ->
source1 ?: source2
}
// THEN
// Observing combined.value yields 1
// WHEN
sourceLiveData1.value = null
// THEN
// Observing combined.value yields 2
Parameters
Transforms five LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.
Consistent with kotlinx.coroutines.flow.combine, combineFunction 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.
For example:
// GIVEN
val sourceLiveData1 = MutableLiveData<Int?>(1)
val sourceLiveData2 = MutableLiveData<Int>(2)
val sourceLiveData3 = MutableLiveData<Int>(3)
val sourceLiveData4 = MutableLiveData<Int>(4)
val sourceLiveData5 = MutableLiveData<Int>(5)
// WHEN
val combined = combine(
sourceLiveData1,
sourceLiveData2,
sourceLiveData3,
sourceLiveData4,
sourceLiveData5
) { source1, source2, source3, source4, source5 ->
source1 ?: source2
}
// THEN
// Observing combined.value yields 1
// WHEN
sourceLiveData1.value = null
// THEN
// Observing combined.value yields 2
Parameters
Transforms a list of LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.
Consistent with kotlinx.coroutines.flow.combine, combineFunction 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 combineFunction is called. While the LiveData instance has an active observer the combineFunction is called for each value set to list.
For example:
// GIVEN
val sources = listOf<LiveData<Int?>>(
MutableLiveData<Int?>(1),
MutableLiveData<Int>(2)
)
// WHEN
val combined = sources.combine { it.filterNotNull().sum() }
// THEN
// Observing combined.value yields 3
// WHEN
sources[0].value = null
// THEN
// Observing combined.value yields 2