# LiveDataScope

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

*** ** * ** ***

<br />

```
public interface LiveDataScope<T extends Object>
```

<br />

*** ** * ** ***

Interface that allows controlling a `https://developer.android.com/reference/androidx/lifecycle/LiveData` from a coroutine block.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/package-summary#liveData(kotlin.coroutines.CoroutineContext,kotlin.Long,kotlin.coroutines.SuspendFunction1)` |   |

## Summary

| ### Public methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emit(kotlin.Any)(@https://developer.android.com/reference/androidx/annotation/NonNull T value)` Set's the `https://developer.android.com/reference/androidx/lifecycle/LiveData`'s value to the given `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emit(kotlin.Any)`. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-disposable-handle/index.html` | `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emitSource(androidx.lifecycle.LiveData)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LiveData<@https://developer.android.com/reference/androidx/annotation/NonNull T> source)` Add the given `https://developer.android.com/reference/androidx/lifecycle/LiveData` as a source, similar to `https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData#addSource(androidx.lifecycle.LiveData<S>,androidx.lifecycle.Observer<? super S>)`. |
| `abstract T` | `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#getLatestValue()()` References the current value of the `https://developer.android.com/reference/androidx/lifecycle/LiveData`. |

## Public methods

### emit

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

```
abstract void emit(@NonNull T value)
```

Set's the `https://developer.android.com/reference/androidx/lifecycle/LiveData`'s value to the given `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emit(kotlin.Any)`. If you've called `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emitSource(androidx.lifecycle.LiveData)` previously, calling `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emit(kotlin.Any)` will remove that source.

Note that this function suspends until the value is set on the `https://developer.android.com/reference/androidx/lifecycle/LiveData`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T value` | The new value for the `https://developer.android.com/reference/androidx/lifecycle/LiveData` |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emitSource(androidx.lifecycle.LiveData)` |   |

### emitSource

```
abstract @NonNull DisposableHandle emitSource(@NonNull LiveData<@NonNull T> source)
```

Add the given `https://developer.android.com/reference/androidx/lifecycle/LiveData` as a source, similar to `https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData#addSource(androidx.lifecycle.LiveData<S>,androidx.lifecycle.Observer<? super S>)`. Calling this method will remove any source that was yielded before via `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emitSource(androidx.lifecycle.LiveData)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LiveData<@https://developer.android.com/reference/androidx/annotation/NonNull T> source` | The `https://developer.android.com/reference/androidx/lifecycle/LiveData` instance whose values will be dispatched from the current `https://developer.android.com/reference/androidx/lifecycle/LiveData`. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emit(kotlin.Any)` |   |
| `https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData#addSource(androidx.lifecycle.LiveData<S>,androidx.lifecycle.Observer<? super S>)` |   |
| `https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData#removeSource(androidx.lifecycle.LiveData<S>)` |   |

### getLatestValue

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

```
abstract T getLatestValue()
```

References the current value of the `https://developer.android.com/reference/androidx/lifecycle/LiveData`.

If the block never `emit`ed a value, `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#latestValue()` will be `null`. You can use this value to check what was then latest value `emit`ed by your `block` before it got cancelled.

Note that if the block called `https://developer.android.com/reference/androidx/lifecycle/LiveDataScope#emitSource(androidx.lifecycle.LiveData)`, then `latestValue` will be last value dispatched by the `source` `https://developer.android.com/reference/androidx/lifecycle/LiveData`.