androidx.paging.rxjava3

Classes

RxPagedListBuilder

This class is deprecated. PagedList is deprecated and has been replaced by PagingData

RxPagingSource

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

RxRemoteMediator

RxJava3 compatibility wrapper around RemoteMediator's suspending APIs.

Extension functions summary

Observable<PagingData<T>>

Operator which caches an Observable of PagingData within a CoroutineScope.

Flowable<PagingData<T>>

Operator which caches a Flowable of PagingData within a CoroutineScope.

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

Returns a PagingData containing only elements matching the given predicate.

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

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

PagingData<R>
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync(generator: (T?, T?) -> Maybe<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).

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

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

Flowable<PagedList<Value>>
<Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
    config: PagedList.Config,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?,
    backpressureStrategy: BackpressureStrategy
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Flowable<PagedList<Value>>
<Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
    config: PagedList.Config,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?,
    backpressureStrategy: BackpressureStrategy
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Flowable<PagedList<Value>>
<Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
    pageSize: Int,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?,
    backpressureStrategy: BackpressureStrategy
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Flowable<PagedList<Value>>
<Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
    pageSize: Int,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?,
    backpressureStrategy: BackpressureStrategy
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Observable<PagedList<Value>>
<Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
    config: PagedList.Config,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Observable<PagedList<Value>>
<Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
    config: PagedList.Config,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Observable<PagedList<Value>>
<Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
    pageSize: Int,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Observable<PagedList<Value>>
<Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
    pageSize: Int,
    initialLoadKey: Key?,
    boundaryCallback: PagedList.BoundaryCallback<Value>?,
    fetchScheduler: Scheduler?,
    notifyScheduler: Scheduler?
)

This function is deprecated. PagedList is deprecated and has been replaced by PagingData

Extension properties summary

Flowable<PagingData<Value>>
Pager<Key, Value>.flowable

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

Observable<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

cachedIn

@ExperimentalCoroutinesApi
fun <T : Any> Observable<PagingData<T>>.cachedIn(scope: CoroutineScope): Observable<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> Flowable<PagingData<T>>.cachedIn(scope: CoroutineScope): Flowable<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

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

Returns a PagingData containing only elements matching the given predicate.

flatMapAsync

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

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

insertSeparatorsAsync

fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(generator: (T?, T?) -> Maybe<R>): 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().uppercaseChar().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().uppercaseChar())
                } else {
                    // no separator - either end of list, or first letters of before/after are the same
                    null
                }
            }.subscribeOn(Schedulers.computation())
        }
}

mapAsync

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

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

toFlowable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
    config: PagedList.Config,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null,
    backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST
): Flowable<PagedList<Value>>

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
config: PagedList.Config

Paging configuration.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / DataSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST

BackpressureStrategy for the Flowable to use.

toFlowable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
    config: PagedList.Config,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null,
    backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST
): Flowable<PagedList<Value>>

Constructs a Flowable<PagedList>, from this PagingSource factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
config: PagedList.Config

Paging configuration.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / PagingSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from PagingSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST

BackpressureStrategy for the Flowable to use.

toFlowable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toFlowable(
    pageSize: Int,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null,
    backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST
): Flowable<PagedList<Value>>

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
pageSize: Int

Page size.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / DataSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST

BackpressureStrategy for the Flowable to use.

toFlowable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toFlowable(
    pageSize: Int,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null,
    backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST
): Flowable<PagedList<Value>>

Constructs a Flowable<PagedList>, from this PagingSource factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
pageSize: Int

Page size.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / PagingSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from PagingSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST

BackpressureStrategy for the Flowable to use.

toObservable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
    config: PagedList.Config,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null
): Observable<PagedList<Value>>

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
config: PagedList.Config

Paging configuration.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / DataSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

toObservable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
    config: PagedList.Config,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null
): Observable<PagedList<Value>>

Constructs a Observable<PagedList> from this PagingSource factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
config: PagedList.Config

Paging configuration.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / PagingSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from PagingSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

toObservable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> DataSource.Factory<Key, Value>.toObservable(
    pageSize: Int,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null
): Observable<PagedList<Value>>

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
pageSize: Int

Size of pages to load.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / DataSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

toObservable

Added in 3.1.0
Deprecated in 3.1.0
fun <Key : Any, Value : Any> () -> PagingSource<Key, Value>.toObservable(
    pageSize: Int,
    initialLoadKey: Key? = null,
    boundaryCallback: PagedList.BoundaryCallback<Value>? = null,
    fetchScheduler: Scheduler? = null,
    notifyScheduler: Scheduler? = null
): Observable<PagedList<Value>>

Constructs a Observable<PagedList> from this PagingSource factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
pageSize: Int

Size of pages to load.

initialLoadKey: Key? = null

Initial load key passed to the first PagedList / PagingSource.

boundaryCallback: PagedList.BoundaryCallback<Value>? = null

The boundary callback for listening to PagedList load state.

fetchScheduler: Scheduler? = null

Scheduler used to fetch from PagingSources, generally a background thread pool for e.g. I/O or network loading.

notifyScheduler: Scheduler? = null

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

Extension properties

val Pager<Key, Value>.flowableFlowable<PagingData<Value>>

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

NOTE: Instances of PagingData emitted by this Flowable 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 PagingData for each downstream observer, you should use the cachedIn operator which multicasts the Flowable in a way that returns a new instance of PagingData with cached data pre-loaded.

val Pager<Key, Value>.observableObservable<PagingData<Value>>

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

NOTE: Instances of PagingData emitted by this Observable 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 PagingData for each downstream observer, you should use the cachedIn operator which multicasts the Observable in a way that returns a new instance of PagingData with cached data pre-loaded.