{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

androidx.paging.rxjava3

{% setvar page_path %}androidx/paging/rxjava3/package-summary.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

Classes

RxPagingSource

Rx-based compatibility wrapper around PagingSource's suspending APIs.

RxRemoteMediator

RxJava3 compatibility wrapper around RemoteMediator's suspending APIs.

Extension properties summary

@ExperimentalCoroutinesApi <ERROR CLASS><PagingData<Value>>
Pager<Key, Value>.flowable

A Flowable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as a Flowable.

@ExperimentalCoroutinesApi <ERROR CLASS><PagingData<Value>>
Pager<Key, Value>.observable

An Observable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as an Observable.

Extension functions summary

@ExperimentalCoroutinesApi <ERROR CLASS><PagingData<T>>

Operator which caches an Observable of PagingData within a CoroutineScope.

@ExperimentalCoroutinesApi <ERROR CLASS><PagingData<T>>

Operator which caches a Flowable of PagingData within a CoroutineScope.

@<ERROR CLASS> PagingData<T>
<T : Any> PagingData<T>.filterAsync(
    predicate: (T) -> <ERROR CLASS><Boolean>
)

Returns a PagingData containing only elements matching the given predicate.

@<ERROR CLASS> PagingData<R>
<T : Any, R : Any> PagingData<T>.flatMapAsync(
    transform: (T) -> <ERROR CLASS><Iterable<R>>
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

@<ERROR CLASS> PagingData<R>
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
    generator: (T?, T?) -> <ERROR CLASS><R>
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

@<ERROR CLASS> PagingData<R>
<T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> <ERROR CLASS><R>)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

Extension properties

flowable

@ExperimentalCoroutinesApi
val Pager<Key, Value>.flowable: @ExperimentalCoroutinesApi <ERROR CLASS><PagingData<Value>>

A Flowable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as a Flowable.

observable

@ExperimentalCoroutinesApi
val Pager<Key, Value>.observable: @ExperimentalCoroutinesApi <ERROR CLASS><PagingData<Value>>

An Observable of PagingData, which mirrors the stream provided by Pager.flow, but exposes it as an Observable.

Extension functions

cachedIn

@ExperimentalCoroutinesApi
fun <T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(scope: CoroutineScope): @ExperimentalCoroutinesApi <ERROR CLASS><PagingData<T>>

Operator which caches an Observable of PagingData within a CoroutineScope.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
scope: CoroutineScope

The CoroutineScope where the page cache will be kept alive. Typically this would be a managed scope such as ViewModel.viewModelScope, which automatically cancels after the PagingData stream is no longer needed. Otherwise, the provided CoroutineScope must be manually cancelled to avoid memory leaks.

cachedIn

@ExperimentalCoroutinesApi
fun <T : Any> <ERROR CLASS><PagingData<T>>.cachedIn(scope: CoroutineScope): @ExperimentalCoroutinesApi <ERROR CLASS><PagingData<T>>

Operator which caches a Flowable of PagingData within a CoroutineScope.

cachedIn multicasts pages loaded and transformed by a PagingData, allowing multiple observers on the same instance of PagingData to receive the same events, avoiding redundant work, but comes at the cost of buffering those pages in memory.

Calling cachedIn is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of PagingData emitted by Pager or any of its transformed derivatives, as reloading data from scratch on the same generation of PagingData is an unsupported operation.

Parameters
scope: CoroutineScope

The CoroutineScope where the page cache will be kept alive. Typically this would be a managed scope such as ViewModel.viewModelScope, which automatically cancels after the PagingData stream is no longer needed. Otherwise, the provided CoroutineScope must be manually cancelled to avoid memory leaks.

filterAsync

@<ERROR CLASS>
fun <T : Any> PagingData<T>.filterAsync(
    predicate: (T) -> <ERROR CLASS><Boolean>
): @<ERROR CLASS> PagingData<T>

Returns a PagingData containing only elements matching the given predicate.

flatMapAsync

@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
    transform: (T) -> <ERROR CLASS><Iterable<R>>
): @<ERROR CLASS> PagingData<R>

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

insertSeparatorsAsync

@<ERROR CLASS>
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
    generator: (T?, T?) -> <ERROR CLASS><R>
): @<ERROR CLASS> PagingData<R>

Returns a PagingData containing each original element, with an optional separator generated by generator, 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.

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().toUpperCase().toString()
            } else {
                // no separator - either end of list, or first letters of before/after are the same
                null
            }
        }.subscribeOn(Schedulers.computation())
    }
}
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().toUpperCase())
                } else {
                    // no separator - either end of list, or first letters of before/after are the same
                    null
                }
            }.subscribeOn(Schedulers.computation())
        }
}

mapAsync

@<ERROR CLASS>
fun <T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> <ERROR CLASS><R>): @<ERROR CLASS> PagingData<R>

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.