# LiveData

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

*** ** * ** ***

Common/AllAndroid/JVM
- android

  ```
  abstract class LiveData<T>
  ```
Known direct subclasses [MutableLiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` which publicly exposes `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#setValue(T)` and `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#postValue(T)` method. |

Known indirect subclasses [MediatorLiveData](https://developer.android.com/reference/kotlin/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. |

*** ** * ** ***

LiveData is a data holder class that can be observed within a given lifecycle. This means that an `https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer` can be added in a pair with a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`, and this observer will be notified about modifications of the wrapped data only if the paired LifecycleOwner is in active state. LifecycleOwner is considered as active, if its state is `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED`. An observer added via `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observeForever(androidx.lifecycle.Observer<? super T>)` is considered as always active and thus will be always notified about modifications. For those observers, you should manually call `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer<? super T>)`.

An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state. This is especially useful for activities and fragments where they can safely observe LiveData and not worry about leaks: they will be instantly unsubscribed when they are destroyed.

In addition, LiveData has `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onActive()` and `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onInactive()` methods to get notified when number of active `https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer`s change between 0 and 1. This allows LiveData to release any heavy resources when it does not have any Observers that are actively observing.

This class is designed to hold individual data fields of `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`, but can also be used for sharing data between different modules in your application in a decoupled fashion.

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` |   |

## Summary

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

| ### Public functions |
|---|---|---|
| `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 |

| ### Protected functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#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/lifecycle/LiveData#onInactive()()` Called when the number of active observers change from 1 to 0. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#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/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#setValue(T)(value: T!)` Sets the value. | android |

| ### Extension functions |
|---|---|---|
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).asFlow()()` Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` containing values dispatched by originating `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData`: at the start a flow collector receives the latest value held by LiveData and then observes LiveData updates. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T?>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).observeAsState()()` Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<R>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, T : R> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)(initial: R)` Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. | android |
| `inline https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.[observe](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).observe(androidx.lifecycle.LifecycleOwner,kotlin.Function1))( owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, crossinline onChanged: (T) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` **This function is deprecated.** This extension method is not required when using Kotlin 1.4. | android |
| `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<T>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).toPublisher(androidx.lifecycle.LifecycleOwner)(lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)` Adapts the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` stream to a ReactiveStreams `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html`. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.Lifecycle)(lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle)` Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within the scope of a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).cachedIn(kotlinx.coroutines.CoroutineScope)(scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html)` Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html`. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.ViewModel)(viewModel: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel)` Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within the scope of a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<X>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <X : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<X>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).distinctUntilChanged()()` Creates a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` object does not emit a value until the source `this` LiveData value has been changed. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <X : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, Y : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<X>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).map(kotlin.Function1)(transform: (X) -> Y)` Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` mapped from `this` LiveData by applying `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).map(kotlin.Function1)` to each value set on `this` LiveData. | android |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread <X : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, Y : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<X>.https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).switchMap(kotlin.Function1)(transform: (X) -> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>?)` Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` mapped from the input `this` `LiveData` by applying `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).switchMap(kotlin.Function1)` to each value set on `this`. | android |

## Public constructors

### LiveData

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

```
LiveData()
```

Creates a LiveData with no value assigned to it.

### LiveData

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

```
LiveData(value: T!)
```

Creates a LiveData initialized with the given `value`.

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

## Public functions

### getValue

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

```
fun getValue(): T?
```

Returns the current value.

Note that calling this method on a background thread does not guarantee that the latest value set will be received.

| Returns |
|---|---|
| `T?` | the current value or null if `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#isInitialized()` is false |

### hasActiveObservers

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

```
fun hasActiveObservers(): Boolean
```

Returns true if this LiveData has active observers.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if this LiveData has active observers |

### hasObservers

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

```
fun hasObservers(): Boolean
```

Returns true if this LiveData has observers.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if this LiveData has observers |

### isInitialized

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

```
fun isInitialized(): Boolean
```

Returns whether an explicit value has been set on this LiveData. If this returns `true`, then the current value can be retrieved from `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#getValue()`.

Note that calling this method on a background thread may still result in this method returning `false` even if a call to `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#postValue(T)` is being processed.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | whether an explicit value has been set on this LiveData |

### observe

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

```
@MainThread
fun observe(owner: LifecycleOwner, observer: Observer<T!>): Unit
```

Adds the given observer to the observers list within the lifespan of the given owner. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the observer.

The observer will only receive events if the owner is in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` state (active).

If the owner moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, the observer will automatically be removed.

When data changes while the `owner` is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.

LiveData keeps a strong reference to the observer and the owner as long as the given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to the observer \&the owner.

If the given owner is already in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, LiveData ignores the call.

If the given owner, observer tuple is already in the list, the call is ignored. If the observer is already in the list with another owner, LiveData throws an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html`.

| Parameters |
|---|---|
| `owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` | The LifecycleOwner which controls the observer |
| `observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>` | The observer that will receive the events |

### observeForever

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

```
@MainThread
fun observeForever(observer: Observer<T!>): Unit
```

Adds the given observer to the observers list. This call is similar to `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observe(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Observer<? super T>)` with a LifecycleOwner, which is always active. This means that the given observer will receive all events and will never be automatically removed. You should manually call `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer<? super T>)` to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active.

If the observer was already added with an owner to this LiveData, LiveData throws an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html`.

| Parameters |
|---|---|
| `observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>` | The observer that will receive the events |

### removeObserver

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

```
@MainThread
fun removeObserver(observer: Observer<T!>): Unit
```

Removes the given observer from the observers list.

| Parameters |
|---|---|
| `observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>` | The Observer to receive events. |

### removeObservers

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

```
@MainThread
fun removeObservers(owner: LifecycleOwner): Unit
```

Removes all observers that are tied to the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner`.

| Parameters |
|---|---|
| `owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` | The `LifecycleOwner` scope for the observers to be removed. |

## Protected functions

### onActive

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

```
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 Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
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()`.

### postValue

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

```
protected fun postValue(value: T!): Unit
```

Posts a task to a main thread to set the given value. So if you have a following code executed in the main thread:

```kotlin
liveData.postValue("a");
liveData.setValue("b");
```
The value "b" would be set at first and later the main thread would override it with the value "a".

If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

| Parameters |
|---|---|
| `value: T!` | The new value |

### setValue

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

```
@MainThread
protected fun setValue(value: T!): Unit
```

Sets the value. If there are active observers, the value will be dispatched to them.

This method must be called from the main thread. If you need set a value from a background thread, you can use `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#postValue(T)`

| Parameters |
|---|---|
| `value: T!` | The new value |

## Extension functions

### LiveData.asFlow

android Artifact: [androidx.lifecycle:lifecycle-livedata](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/FlowLiveData.kt+function:asFlow)

```
fun <T : Any?> LiveData<T>.asFlow(): Flow<T>
```

Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` containing values dispatched by originating `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData`: at the start a flow collector receives the latest value held by LiveData and then observes LiveData updates.

When a collection of the returned flow starts the originating `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` becomes `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onActive()`. Similarly, when a collection completes `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` becomes `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onInactive()`.

BackPressure: the returned flow is conflated. There is no mechanism to suspend an emission by LiveData due to a slow collector, so collector always gets the most recent value emitted.

### LiveData.observeAsState

android Artifact: [androidx.compose.runtime:runtime-livedata](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState)

```
@Composable
fun <T : Any?> LiveData<T>.observeAsState(): State<T?>
```

Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. Every time there would be new value posted into the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

The inner observer will automatically be removed when this composable disposes or the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String? by liveData.observeAsState()
Text("Value is $value")
```

### LiveData.observeAsState

android Artifact: [androidx.compose.runtime:runtime-livedata](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState)

```
@Composable
fun <R : Any?, T : R> LiveData<T>.observeAsState(initial: R): State<R>
```

Starts observing this `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` and represents its values via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State`. Every time there would be new value posted into the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` will be updated causing recomposition of every `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` usage.

The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)` value will be used only if this LiveData is not already `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#isInitialized()`. Note that if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)` is a non-null type, it is your responsibility to ensure that any value you set on this LiveData is also non-null.

The inner observer will automatically be removed when this composable disposes or the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner` moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state.

```kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState

val value: String by liveData.observeAsState("initial")
Text("Value is $value")
```

### LiveData.observe

android Artifact: [androidx.lifecycle:lifecycle-livedata-core](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/LiveData.kt+function:observe)

```
@MainThread
inline fun <T : Any?> LiveData<T>.[observe](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#(androidx.lifecycle.LiveData).observe(androidx.lifecycle.LifecycleOwner,kotlin.Function1))(
    owner: LifecycleOwner,
    crossinline onChanged: (T) -> Unit
): Observer<T>
```

> [!CAUTION]
> **This function is deprecated.**   
> This extension method is not required when using Kotlin 1.4. You should remove "import androidx.lifecycle.observe"

Adds the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).observe(androidx.lifecycle.LifecycleOwner,kotlin.Function1)` lambda as an observer within the lifespan of the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).observe(androidx.lifecycle.LifecycleOwner,kotlin.Function1)` and returns a reference to observer. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the onChanged.

The observer will only receive events if the owner is in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#STARTED` or `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#RESUMED` state (active).

If the owner moves to the `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, the observer will automatically be removed.

When data changes while the `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).observe(androidx.lifecycle.LifecycleOwner,kotlin.Function1)` is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.

LiveData keeps a strong reference to the observer and the owner as long as the given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to the observer and the owner.

If the given owner is already in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` state, LiveData ignores the call.

### LiveData.toPublisher

android Artifact: [androidx.lifecycle:lifecycle-reactivestreams](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/LiveDataReactiveStreams.kt+function:toPublisher)

```
fun <T : Any?> LiveData<T>.toPublisher(lifecycle: LifecycleOwner): Publisher<T>
```

Adapts the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` stream to a ReactiveStreams `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html`.

By using a good publisher implementation such as RxJava 2.x Flowables, most consumers will be able to let the library deal with backpressure using operators and not need to worry about ever manually calling `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Subscription.html#request(long)`.

On subscription to the publisher, the observer will attach to the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData`. Once `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Subscription.html#request(long)` is called on the subscription object, an observer will be connected to the data stream. Calling `request(Long.MAX_VALUE)` is equivalent to creating an unbounded stream with no backpressure. If request with a finite count reaches 0, the observer will buffer the latest item and emit it to the subscriber when data is again requested. Any other items emitted during the time there was no backpressure requested will be dropped.

### LiveData.cachedIn

android Artifact: [androidx.paging:paging-runtime](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/PagingLiveData.kt+function:cachedIn)

```
fun <T : Any> LiveData<PagingData<T>>.cachedIn(lifecycle: Lifecycle): LiveData<PagingData<T>>
```

Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within the scope of a `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle`.

`https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.Lifecycle)` multicasts pages loaded and transformed by a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, allowing multiple observers on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling `https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.Lifecycle)` is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` emitted by `https://developer.android.com/reference/kotlin/androidx/paging/Pager` or any of its transformed derivatives, as reloading data from scratch on the same generation of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` is an unsupported operation.

| Parameters |
|---|---|
| `lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` where the page cache will be kept alive. |

### LiveData.cachedIn

android Artifact: [androidx.paging:paging-runtime](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/PagingLiveData.kt+function:cachedIn)

```
fun <T : Any> LiveData<PagingData<T>>.cachedIn(scope: CoroutineScope): LiveData<PagingData<T>>
```

Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html`.

`https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(kotlinx.coroutines.CoroutineScope)` multicasts pages loaded and transformed by a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, allowing multiple observers on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling `https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(kotlinx.coroutines.CoroutineScope)` is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` emitted by `https://developer.android.com/reference/kotlin/androidx/paging/Pager` or any of its transformed derivatives, as reloading data from scratch on the same generation of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` is an unsupported operation.

| Parameters |
|---|---|
| `scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` where the page cache will be kept alive. Typically this would be a managed scope such as `ViewModel.viewModelScope`, which automatically cancels after the `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` stream is no longer needed. Otherwise, the provided `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` must be manually cancelled to avoid memory leaks. |

### LiveData.cachedIn

android Artifact: [androidx.paging:paging-runtime](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/PagingLiveData.kt+function:cachedIn)

```
fun <T : Any> LiveData<PagingData<T>>.cachedIn(viewModel: ViewModel): LiveData<PagingData<T>>
```

Operator which caches a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` within the scope of a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel`.

`https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.ViewModel)` multicasts pages loaded and transformed by a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, allowing multiple observers on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling `https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.ViewModel)` is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` emitted by `https://developer.android.com/reference/kotlin/androidx/paging/Pager` or any of its transformed derivatives, as reloading data from scratch on the same generation of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` is an unsupported operation.

| Parameters |
|---|---|
| `viewModel: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` | The `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` whose `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModel).viewModelScope()` will dictate how long the page cache will be kept alive. |

### LiveData.distinctUntilChanged

android Artifact: [androidx.lifecycle:lifecycle-livedata](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Transformations.kt+function:distinctUntilChanged)

```
@MainThread
fun <X : Any?> LiveData<X>.distinctUntilChanged(): LiveData<X>
```

Creates a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` object does not emit a value until the source `this` LiveData value has been changed. The value is considered changed if `equals()` yields `false`.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<X>` | a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` of type `X` |

### LiveData.map

android Artifact: [androidx.lifecycle:lifecycle-livedata](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Transformations.kt+function:map)

```
@MainThread
fun <X : Any?, Y : Any?> LiveData<X>.map(transform: (X) -> Y): LiveData<Y>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` mapped from `this` LiveData by applying `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).map(kotlin.Function1)` to each value set on `this` LiveData.

This method is analogous to `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html#map-io.reactivex.functions.Function&lt;? super T,? extends R&gt;-`.

`https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).map(kotlin.Function1)` will be executed on the main thread.

Here is an example mapping a simple `User` struct in a `LiveData` to a `LiveData` containing their full name as a `String`.

```kotlin
val userLD : LiveData<User> = ...;
val userFullNameLD: LiveData<String> = userLD.map { user -> user.firstName + user.lastName }
```

| Parameters |
|---|---|
| `transform: (X) -> Y` | a function to apply to each value set on `source` in order to set it on the output `LiveData` |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>` | a LiveData mapped from `source` to type `<Y>` by applying `mapFunction` to each value set. |

### LiveData.switchMap

android Artifact: [androidx.lifecycle:lifecycle-livedata](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/Transformations.kt+function:switchMap)

```
@MainThread
fun <X : Any?, Y : Any?> LiveData<X>.switchMap(transform: (X) -> LiveData<Y>?): LiveData<Y>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` mapped from the input `this` `LiveData` by applying `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).switchMap(kotlin.Function1)` to each value set on `this`.


The returned \`LiveData\` delegates to the most recent \`LiveData\` created by \[transform\] with the
most recent value set to \`this\`, without changing the reference. In this way \[transform\] can
change the 'backing' \`LiveData\` transparently to any observer registered to the \`LiveData\`
returned by \`switchMap()\`.

Note that when the backing `LiveData` is switched, no further values from the older `LiveData` will be set to the output `LiveData`. In this way, the method is analogous to `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html#switchMap-io.reactivex.functions.Function&lt;? super T,? extends io.reactivex.ObservableSource&lt;? extends R&gt;&gt;-`.

`https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LiveData).switchMap(kotlin.Function1)` will be executed on the main thread.

Here is an example class that holds a typed-in name of a user `String` (such as from an `EditText`) in a `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData` and returns a `LiveData` containing a List of `User` objects for users that have that name. It populates that `LiveData` by requerying a repository-pattern object each time the typed name changes.


This \`ViewModel\` would permit the observing UI to update "live" as the user ID text changes.

```kotlin
class UserViewModel: AndroidViewModel {
    val nameQueryLiveData : MutableLiveData<String> = ...

    fun usersWithNameLiveData(): LiveData<List<String>> = nameQueryLiveData.switchMap {
        name -> myDataSource.usersWithNameLiveData(name)
    }

    fun setNameQuery(val name: String) {
        this.nameQueryLiveData.value = name;
    }
}
```

| Parameters |
|---|---|
| `transform: (X) -> https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>?` | a function to apply to each value set on `source` to create a new delegate `LiveData` for the returned one |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<Y>` | a LiveData mapped from `source` to type `<Y>` by delegating to the LiveData returned by applying `switchMapFunction` to each value set |