# LiveDataReactiveStreams

Added in 2.0.0

*** ** * ** ***


```
public final class LiveDataReactiveStreams
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `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/LiveDataReactiveStreams#(org.reactivestreams.Publisher).fromPublisher()(@https://developer.android.com/reference/androidx/annotation/NonNull https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver)` Creates an observable `https://developer.android.com/reference/androidx/lifecycle/LiveData` stream from a ReactiveStreams `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html`}. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> [toPublisher](https://developer.android.com/reference/androidx/lifecycle/LiveDataReactiveStreams#toPublisher(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.LiveData))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner lifecycle, @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> liveData )` **This method is deprecated.** Use the extension method \`liveData.toPublisher(lifecycleOwner)\` instead. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.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/LiveDataReactiveStreams#(androidx.lifecycle.LiveData).toPublisher(androidx.lifecycle.LifecycleOwner)( @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, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner lifecycle )` Adapts the given `https://developer.android.com/reference/androidx/lifecycle/LiveData` stream to a ReactiveStreams `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html`. |

## Public methods

### fromPublisher

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

```
public static final @NonNull LiveData<@NonNull T> <T extends Object> fromPublisher(@NonNull Publisher<@NonNull T> receiver)
```

Creates an observable `https://developer.android.com/reference/androidx/lifecycle/LiveData` stream from a ReactiveStreams `https://www.reactive-streams.org/reactive-streams-1.0.4-javadoc/org/reactivestreams/Publisher.html`}.

When the LiveData becomes active, it subscribes to the emissions from the Publisher.

When the LiveData becomes inactive, the subscription is cleared. LiveData holds the last value emitted by the Publisher when the LiveData was active.

Therefore, in the case of a hot RxJava Observable, when a new LiveData `https://developer.android.com/reference/androidx/lifecycle/Observer` is added, it will automatically notify with the last value held in LiveData, which might not be the last value emitted by the Publisher.

Note that LiveData does NOT handle errors and it expects that errors are treated as states in the data that's held. In case of an error being emitted by the publisher, an error will be propagated to the main thread and the app will crash.

### toPublisher

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

```
public static final @NonNull Publisher<@NonNull T> <T extends Object> [toPublisher](https://developer.android.com/reference/androidx/lifecycle/LiveDataReactiveStreams#toPublisher(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.LiveData))(
    @NonNull LifecycleOwner lifecycle,
    @NonNull LiveData<@NonNull T> liveData
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Use the extension method \`liveData.toPublisher(lifecycleOwner)\` instead, use this instead:
>
> ```
> liveData.toPublisher(lifecycleOwner)
> ```

Adapts the given `https://developer.android.com/reference/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/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.

### toPublisher

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

```
public static final @NonNull Publisher<@NonNull T> <T extends Object> toPublisher(
    @NonNull LiveData<@NonNull T> receiver,
    @NonNull LifecycleOwner lifecycle
)
```

Adapts the given `https://developer.android.com/reference/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/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.