# MediatorLiveData

Artifact: [androidx.lifecycle:lifecycle-livedata](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/MediatorLiveData.java+class:androidx.lifecycle.MediatorLiveData) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***

Common/AllAndroid/JVM
- android

  ```
  class MediatorLiveData<T> : MutableLiveData<T!>
  ```

|---|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) ||||
| ↳ | [androidx.lifecycle.LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData) |||
|   | ↳ | [androidx.lifecycle.MutableLiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData)\<T!\> ||
|   |   | ↳ | [androidx.lifecycle.MediatorLiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData) |

*** ** * ** ***

`https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` subclass which may observe other `LiveData` objects and react on `OnChanged` events from them.

This class correctly propagates its active/inactive states down to source `LiveData` objects.

Consider the following scenario: we have 2 instances of `LiveData`, let's name them `liveData1` and `liveData2`, and we want to merge their emissions in one `https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData` object: `liveDataMerger`. Then, `liveData1` and `
liveData2` will become sources for the `liveDataMerger` and every time `onChanged` callback is called for either of them, we set a new value in `liveDataMerger`.

```kotlin
LiveData<Integer> liveData1 = ...;
LiveData<Integer> liveData2 = ...;

MediatorLiveData<Integer> liveDataMerger = new MediatorLiveData<>();
liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value));
liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
```

Let's consider that we only want 10 values emitted by `liveData1`, to be merged in the `liveDataMerger`. Then, after 10 values, we can stop listening to `
liveData1` and remove it as a source.

```kotlin
liveDataMerger.addSource(liveData1, new Observer<Integer>() {
     private int count = 1;

     @Override public void onChanged(@Nullable Integer s) {
         count++;
         liveDataMerger.setValue(s);
         if (count > 10) {
             liveDataMerger.removeSource(liveData1);
         }
     }
});
```

| Parameters |
|---|---|
| `<T>` | The type of data hold by this instance |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#MediatorLiveData()()` Creates a MediatorLiveData with no value assigned to it. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#MediatorLiveData(T)(value: T!)` Creates a MediatorLiveData initialized with the given `value`. | android |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <S> https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#addSource(androidx.lifecycle.LiveData<S>,androidx.lifecycle.Observer<? super S>)(source: https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<S!>, onChanged: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<S!>)` Starts to listen to the given `source` LiveData, `onChanged` observer will be called when `source` value was changed. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <S> https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#removeSource(androidx.lifecycle.LiveData<S>)(toRemote: https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<S!>)` Stops to listen the given `LiveData`. | android |

| ### Protected functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/CallSuper https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#onActive()()` Called when the number of active observers change from 0 to 1. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/CallSuper https://developer.android.com/reference/kotlin/androidx/lifecycle/MediatorLiveData#onInactive()()` Called when the number of active observers change from 1 to 0. | android |

### Inherited functions

From [androidx.lifecycle.LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData) `T?` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#getValue()()`

Returns the current value.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#hasActiveObservers()()`

Returns true if this LiveData has active observers.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#hasObservers()()`

Returns true if this LiveData has observers.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#isInitialized()()`

Returns whether an explicit value has been set on this LiveData.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observe(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Observer<? super T>)(owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Adds the given observer to the observers list within the lifespan of the given owner.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observeForever(androidx.lifecycle.Observer<? super T>)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Adds the given observer to the observers list.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer<? super T>)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Removes the given observer from the observers list.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObservers(androidx.lifecycle.LifecycleOwner)(owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)`

Removes all observers that are tied to the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`.
android From [androidx.lifecycle.MutableLiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#postValue(T)(value: T!)`

Posts a task to a main thread to set the given value.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#setValue(T)(value: T!)`

Sets the value.
android

## Public constructors

### MediatorLiveData

android Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
MediatorLiveData()
```

Creates a MediatorLiveData with no value assigned to it.

### MediatorLiveData

android Added in [2.6.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.6.0)

```
MediatorLiveData(value: T!)
```

Creates a MediatorLiveData initialized with the given `value`.

| Parameters |
|---|---|
| `value: T!` | initial value |

## Public functions

### addSource

android Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
fun <S> addSource(source: LiveData<S!>, onChanged: Observer<S!>): Unit
```

Starts to listen to the given `source` LiveData, `onChanged` observer will be called when `source` value was changed.

`onChanged` callback will be called only when this `MediatorLiveData` is active.

If the given LiveData is already added as a source but with a different Observer, `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` will be thrown.

| Parameters |
|---|---|
| `<S>` | The type of data hold by `source` LiveData |
| `source: https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<S!>` | the `LiveData` to listen to |
| `onChanged: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<S!>` | The observer that will receive the events |

### removeSource

android Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
@MainThread
fun <S> removeSource(toRemote: LiveData<S!>): Unit
```

Stops to listen the given `LiveData`.

| Parameters |
|---|---|
| `<S>` | the type of data hold by `source` LiveData |
| `toRemote: https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<S!>` | `LiveData` to stop to listen |

## Protected functions

### onActive

android

```
@CallSuper
protected fun onActive(): Unit
```

Called when the number of active observers change from 0 to 1.

This callback can be used to know that this LiveData is being used thus should be kept up to date.

### onInactive

android

```
@CallSuper
protected fun onInactive(): Unit
```

Called when the number of active observers change from 1 to 0.

This does not mean that there are no observers left, there may still be observers but their lifecycle states aren't `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` (like an Activity in the back stack).

You can check if there are observers via `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#hasObservers()`.