# FlowLiveDataConversions

Added in 2.7.0

*** ** * ** ***


```
public final class FlowLiveDataConversions
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/lifecycle/FlowLiveDataConversions#(androidx.lifecycle.LiveData).asFlow()(@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> receiver)` 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/androidx/lifecycle/LiveData`: at the start a flow collector receives the latest value held by LiveData and then observes LiveData updates. |
| `static final @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>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/lifecycle/FlowLiveDataConversions#(kotlinx.coroutines.flow.Flow).asLiveData(kotlin.coroutines.CoroutineContext,kotlin.Long)( @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html context, long timeoutInMs )` Creates a LiveData that has values collected from the origin `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`. |
| `static final @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>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) <T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/lifecycle/FlowLiveDataConversions#(kotlinx.coroutines.flow.Flow).asLiveData(java.time.Duration,kotlin.coroutines.CoroutineContext)( @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html timeout, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html context )` Creates a LiveData that has values collected from the origin `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`. |

## Public methods

### asFlow

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

```
public static final @NonNull Flow<@NonNull T> <T extends Object> asFlow(@NonNull LiveData<@NonNull T> receiver)
```

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/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/androidx/lifecycle/LiveData` becomes `https://developer.android.com/reference/androidx/lifecycle/LiveData#onActive()`. Similarly, when a collection completes `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes `https://developer.android.com/reference/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.

### asLiveData

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:asLiveData)

```
public static final @NonNull LiveData<@NonNull T> <T extends Object> asLiveData(
    @NonNull Flow<@NonNull T> receiver,
    @NonNull CoroutineContext context,
    long timeoutInMs
)
```

Creates a LiveData that has values collected from the origin `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`.

If the origin `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` is a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html`, then the initial value will be populated to the `https://developer.android.com/reference/androidx/lifecycle/LiveData`'s value field on the main thread.

The upstream flow collection starts when the returned `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active (`https://developer.android.com/reference/androidx/lifecycle/LiveData#onActive()`). If the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes inactive (`https://developer.android.com/reference/androidx/lifecycle/LiveData#onInactive()`) while the flow has not completed, the flow collection will be cancelled after `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(kotlin.coroutines.CoroutineContext,kotlin.Long)` milliseconds unless the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active again before that timeout (to gracefully handle cases like Activity rotation).

After a cancellation, if the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active again, the upstream flow collection will be re-executed.

If the upstream flow completes successfully *or* is cancelled due to reasons other than `https://developer.android.com/reference/androidx/lifecycle/LiveData` becoming inactive, it *will not* be re-collected even after `https://developer.android.com/reference/androidx/lifecycle/LiveData` goes through active inactive cycle.

If flow completes with an exception, then exception will be delivered to the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-exception-handler/index.html` of provided `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(kotlin.coroutines.CoroutineContext,kotlin.Long)`. By default `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-empty-coroutine-context/index.html` is used to so an exception will be delivered to main's thread `https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler.html`. If your flow upstream is expected to throw, you can use `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/index.html` on upstream flow to emit a helpful error object.

The `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(kotlin.coroutines.CoroutineContext,kotlin.Long)` can be changed to fit different use cases better, for example increasing it will give more time to flow to complete before being canceled and is good for finite flows that are costly to restart. Otherwise if a flow is cheap to restart decreasing the `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(kotlin.coroutines.CoroutineContext,kotlin.Long)` value will allow to produce less values that aren't consumed by anything.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html context` | The CoroutineContext to collect the upstream flow in. Defaults to `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-empty-coroutine-context/index.html` combined with `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.html` |
| `long timeoutInMs` | The timeout in ms before cancelling the block if there are no active observers (`https://developer.android.com/reference/androidx/lifecycle/LiveData#hasActiveObservers()`. Defaults to `https://developer.android.com/reference/androidx/lifecycle/package-summary#DEFAULT_TIMEOUT()`. |

### asLiveData

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:asLiveData)

```
@RequiresApi(value = 26)
public static final @NonNull LiveData<@NonNull T> <T extends Object> asLiveData(
    @NonNull Flow<@NonNull T> receiver,
    @NonNull Duration timeout,
    @NonNull CoroutineContext context
)
```

Creates a LiveData that has values collected from the origin `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`.

The upstream flow collection starts when the returned `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active (`https://developer.android.com/reference/androidx/lifecycle/LiveData#onActive()`). If the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes inactive (`https://developer.android.com/reference/androidx/lifecycle/LiveData#onInactive()`) while the flow has not completed, the flow collection will be cancelled after `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(java.time.Duration,kotlin.coroutines.CoroutineContext)` unless the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active again before that timeout (to gracefully handle cases like Activity rotation).

After a cancellation, if the `https://developer.android.com/reference/androidx/lifecycle/LiveData` becomes active again, the upstream flow collection will be re-executed.

If the upstream flow completes successfully *or* is cancelled due to reasons other than `https://developer.android.com/reference/androidx/lifecycle/LiveData` becoming inactive, it *will not* be re-collected even after `https://developer.android.com/reference/androidx/lifecycle/LiveData` goes through active inactive cycle.

If flow completes with an exception, then exception will be delivered to the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-exception-handler/index.html` of provided `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(java.time.Duration,kotlin.coroutines.CoroutineContext)`. By default `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-empty-coroutine-context/index.html` is used to so an exception will be delivered to main's thread `https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler.html`. If your flow upstream is expected to throw, you can use `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/index.html` on upstream flow to emit a helpful error object.

The `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(java.time.Duration,kotlin.coroutines.CoroutineContext)` can be changed to fit different use cases better, for example increasing it will give more time to flow to complete before being canceled and is good for finite flows that are costly to restart. Otherwise if a flow is cheap to restart decreasing the `https://developer.android.com/reference/androidx/lifecycle/package-summary#(kotlinx.coroutines.flow.Flow).asLiveData(java.time.Duration,kotlin.coroutines.CoroutineContext)` value will allow to produce less values that aren't consumed by anything.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html timeout` | The timeout in ms before cancelling the block if there are no active observers (`https://developer.android.com/reference/androidx/lifecycle/LiveData#hasActiveObservers()`. Defaults to `https://developer.android.com/reference/androidx/lifecycle/package-summary#DEFAULT_TIMEOUT()`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html context` | The CoroutineContext to collect the upstream flow in. Defaults to `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-empty-coroutine-context/index.html` combined with `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.html` |