# androidx.paging.rxjava2

*** ** * ** ***

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxPagingSource` | Rx-based compatibility wrapper around `https://developer.android.com/reference/kotlin/androidx/paging/PagingSource`'s suspending APIs. |
| `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/RxRemoteMediator` | RxJava2 compatibility wrapper around `https://developer.android.com/reference/kotlin/androidx/paging/RemoteMediator`'s suspending APIs. |

## Extension functions summary

|---|---|
| `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>` | `@https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-experimental-coroutines-api/index.html <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(io.reactivex.Observable).cachedIn(kotlinx.coroutines.CoroutineScope)(scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html)` Operator which caches an `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` 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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>` | `@https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-experimental-coroutines-api/index.html <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(io.reactivex.Flowable).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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` 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/PagingData<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).filterAsync(kotlin.Function1)(predicate: (T) -> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html>)` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing only elements matching the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).filterAsync(kotlin.Function1)`. |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingData<R>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).flatMapAsync(kotlin.Function1)( transform: (T) -> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html<R>> )` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` of all elements returned from applying the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).flatMapAsync(kotlin.Function1)` to each element, as it is loaded. |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingData<R>` | `<T : R, R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).insertSeparatorsAsync(kotlin.Function2)( generator: (T?, T?) -> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Maybe.html<R> )` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing each original element, with an optional separator generated by `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).insertSeparatorsAsync(kotlin.Function2)`, given the elements before and after (or null, in boundary conditions). |
| `https://developer.android.com/reference/kotlin/androidx/paging/PagingData<R>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html, R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/paging/PagingData<T>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).mapAsync(kotlin.Function1)(transform: (T) -> https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<R>)` Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing the result of applying the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).mapAsync(kotlin.Function1)` to each element, as it is loaded. |

## Extension properties summary

|---|---|
| `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<Value>>` | `https://developer.android.com/reference/kotlin/androidx/paging/Pager<Key, Value>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.Pager).flowable()` A `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, which mirrors the stream provided by `https://developer.android.com/reference/kotlin/androidx/paging/Pager#flow()`, but exposes it as a `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html`. |
| `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html<https://developer.android.com/reference/kotlin/androidx/paging/PagingData<Value>>` | `https://developer.android.com/reference/kotlin/androidx/paging/Pager<Key, Value>.https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.Pager).observable()` An `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, which mirrors the stream provided by `https://developer.android.com/reference/kotlin/androidx/paging/Pager#flow()`, but exposes it as an `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html`. |

## Extension functions

### Observable.cachedIn

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/PagingRx.kt+function:cachedIn) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
@ExperimentalCoroutinesApi
fun <T : Any> Observable<PagingData<T>>.cachedIn(scope: CoroutineScope): Observable<PagingData<T>>
```

Operator which caches an `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` 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/rxjava2/package-summary#(io.reactivex.Observable).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/rxjava2/package-summary#(io.reactivex.Observable).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. |

### Flowable.cachedIn

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/PagingRx.kt+function:cachedIn) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
@ExperimentalCoroutinesApi
fun <T : Any> Flowable<PagingData<T>>.cachedIn(scope: CoroutineScope): Flowable<PagingData<T>>
```

Operator which caches a `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` 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/rxjava2/package-summary#(io.reactivex.Flowable).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/rxjava2/package-summary#(io.reactivex.Flowable).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. |

### PagingData.filterAsync

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/RxPagingData.kt+function:filterAsync) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
fun <T : Any> PagingData<T>.filterAsync(predicate: (T) -> Single<Boolean>): PagingData<T>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing only elements matching the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).filterAsync(kotlin.Function1)`.

### PagingData.flatMapAsync

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/RxPagingData.kt+function:flatMapAsync) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
    transform: (T) -> Single<Iterable<R>>
): PagingData<R>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` of all elements returned from applying the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).flatMapAsync(kotlin.Function1)` to each element, as it is loaded.

### PagingData.insertSeparatorsAsync

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/RxPagingData.kt+function:insertSeparatorsAsync) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
    generator: (T?, T?) -> Maybe<R>
): PagingData<R>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing each original element, with an optional separator generated by `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).insertSeparatorsAsync(kotlin.Function2)`, given the elements before and after (or null, in boundary conditions).

Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.

```kotlin
import androidx.paging.insertSeparators
import androidx.paging.insertSeparatorsAsync
import androidx.paging.rxjava2.insertSeparatorsAsync

/*
 * Create letter separators in an alphabetically sorted list.
 *
 * For example, if the input is:
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData.insertSeparatorsAsync { before: String?, after: String? ->
        Maybe.fromCallable<String> {
                if (after != null && before?.first() != after.first()) {
                    // separator - after is first item that starts with its first letter
                    after.first().uppercaseChar().toString()
                } else {
                    // no separator - either end of list, or first letters of before/after are
                    // the same
                    null
                }
            }
            .subscribeOn(Schedulers.computation())
    }
}
```

```kotlin
import androidx.paging.insertSeparators
import androidx.paging.insertSeparatorsAsync
import androidx.paging.map
import androidx.paging.rxjava2.insertSeparatorsAsync

open class UiModel
data class ItemUiModel(val item: Item) : UiModel()
data class SeparatorUiModel(val char: Char) : UiModel()

/*
 * Create letter separators in an alphabetically sorted list of Items, with UiModel objects.
 *
 * For example, if the input is (each an `Item`):
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output a list of UiModels corresponding to:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData
        .map { item ->
            ItemUiModel(item) // convert items in stream to ItemUiModel
        }
        .insertSeparatorsAsync { before: ItemUiModel?, after: ItemUiModel? ->
            Maybe.fromCallable<UiModel> {
                    if (
                        after != null &&
                            before?.item?.label?.first() != after.item.label.first()
                    ) {
                        // separator - after is first item that starts with its first letter
                        SeparatorUiModel(after.item.label.first().uppercaseChar())
                    } else {
                        // no separator - either end of list, or first letters of before/after
                        // are the same
                        null
                    }
                }
                .subscribeOn(Schedulers.computation())
        }
}
```

### PagingData.mapAsync

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/RxPagingData.kt+function:mapAsync) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
fun <T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> Single<R>): PagingData<R>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` containing the result of applying the given `https://developer.android.com/reference/kotlin/androidx/paging/rxjava2/package-summary#(androidx.paging.PagingData).mapAsync(kotlin.Function1)` to each element, as it is loaded.

## Extension properties

### Pager.flowable

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/PagingRx.kt+symbol:flowable)

```
val Pager<Key, Value>.flowable: Flowable<PagingData<Value>>
```

A `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, which mirrors the stream provided by `https://developer.android.com/reference/kotlin/androidx/paging/Pager#flow()`, but exposes it as a `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html`.

NOTE: Instances of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` emitted by this `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` are not re-usable and cannot be submitted multiple times. This is especially relevant for transforms, which would replay the latest value downstream. To ensure you get a new instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` for each downstream observer, you should use the `https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.Lifecycle)` operator which multicasts the `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html` in a way that returns a new instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` with cached data pre-loaded.

### Pager.observable

Artifact: [androidx.paging:paging-rxjava2](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/rxjava2/PagingRx.kt+symbol:observable)

```
val Pager<Key, Value>.observable: Observable<PagingData<Value>>
```

An `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData`, which mirrors the stream provided by `https://developer.android.com/reference/kotlin/androidx/paging/Pager#flow()`, but exposes it as an `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html`.

NOTE: Instances of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` emitted by this `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` are not re-usable and cannot be submitted multiple times. This is especially relevant for transforms, which would replay the latest value downstream. To ensure you get a new instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` for each downstream observer, you should use the `https://developer.android.com/reference/kotlin/androidx/paging/package-summary#(androidx.lifecycle.LiveData).cachedIn(androidx.lifecycle.Lifecycle)` operator which multicasts the `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html` in a way that returns a new instance of `https://developer.android.com/reference/kotlin/androidx/paging/PagingData` with cached data pre-loaded.