combine

fun <S1, S2, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, combineFunction: (S1, S2) -> T): LiveData<T>

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

S1

The type of the source1 values.

S2

The type of the source2 values.

T

The type of the returned LiveData value.

combineFunction

A function that maps source values to T.


fun <S1, S2, S3, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, combineFunction: (S1, S2, S3) -> T): LiveData<T>

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

S1

The type of the source1 values.

S2

The type of the source2 values.

S3

The type of the source3 values.

T

The type of the returned LiveData value.

combineFunction

A function that maps source values to T.


fun <S1, S2, S3, S4, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, source4: LiveData<out S4>, combineFunction: (S1, S2, S3, S4) -> T): LiveData<T>

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

S1

The type of the source1 values.

S2

The type of the source2 values.

S3

The type of the source3 values.

S4

The type of the source4 values.

T

The type of the returned LiveData value.

combineFunction

A function that maps source values to T.


fun <S1, S2, S3, S4, S5, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, source4: LiveData<out S4>, source5: LiveData<out S5>, combineFunction: (S1, S2, S3, S4, S5) -> T): LiveData<T>

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

S1

The type of the source1 values.

S2

The type of the source2 values.

S3

The type of the source3 values.

S4

The type of the source4 values.

S5

The type of the source5 values.

T

The type of the returned LiveData value.

combineFunction

A function that maps source values to T.


fun <S, T> Collection<LiveData<out S>>.combine(combineFunction: (List<S>) -> T): LiveData<T>

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

Parameters

S

The type of the source LiveData values.

T

The type of the returned LiveData value.

combineFunction

A function that maps source values to T.