public final class Pager<Key extends Object, Value extends Object>


Primary entry point into Paging; constructor for a reactive stream of PagingData. The same Pager instance should be reused within an instance of ViewModel. For example in your ViewModel:

// create a Pager instance and store to a variable
val
pager = Pager(
...
)
.flow
.cachedIn(viewModelScope)

Each PagingData represents a snapshot of the backing paginated data. Updates to the backing dataset should be represented by a new instance of PagingData.

PagingSource.invalidate and calls to AsyncPagingDataDiffer.refresh or PagingDataAdapter.refresh will notify Pager that the backing dataset has been updated and a new PagingData / PagingSource pair will be generated to represent an updated snapshot.

PagingData can be transformed to alter data as it loads, and presented in a RecyclerView via AsyncPagingDataDiffer or PagingDataAdapter.

LiveData support is available as an extension property provided by the androidx.paging:paging-runtime artifact.

RxJava support is available as extension properties provided by the androidx.paging:paging-rxjava2 artifact.

Summary

Public constructors

<Key extends Object, Value extends Object> Pager(
    @NonNull PagingConfig config,
    Key initialKey,
    @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> pagingSourceFactory
)
@ExperimentalPagingApi
<Key extends Object, Value extends Object> Pager(
    @NonNull PagingConfig config,
    Key initialKey,
    RemoteMediator<@NonNull Key, @NonNull Value> remoteMediator,
    @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> pagingSourceFactory
)

Public methods

final @NonNull Flow<@NonNull PagingData<@NonNull Value>>

A cold Flow of PagingData, which emits new instances of PagingData once they become invalidated by PagingSource.invalidate or calls to AsyncPagingDataDiffer.refresh or PagingDataAdapter.refresh.

Extension functions

final @NonNull LiveData<@NonNull PagingData<@NonNull Value>>
PagingLiveDataKt.getLiveData(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

final @NonNull Flowable<@NonNull PagingData<@NonNull Value>>
PagingRxKt.getFlowable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

final @NonNull Flowable<@NonNull PagingData<@NonNull Value>>
PagingRxKt.getFlowable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

final @NonNull Observable<@NonNull PagingData<@NonNull Value>>
PagingRxKt.getObservable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

final @NonNull Observable<@NonNull PagingData<@NonNull Value>>
PagingRxKt.getObservable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

Public constructors

Pager

public <Key extends Object, Value extends Object> Pager(
    @NonNull PagingConfig config,
    Key initialKey,
    @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> pagingSourceFactory
)

Pager

@ExperimentalPagingApi
public <Key extends Object, Value extends Object> Pager(
    @NonNull PagingConfig config,
    Key initialKey,
    RemoteMediator<@NonNull Key, @NonNull Value> remoteMediator,
    @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> pagingSourceFactory
)

Public methods

getFlow

Added in 3.0.0
public final @NonNull Flow<@NonNull PagingData<@NonNull Value>> getFlow()

A cold Flow of PagingData, which emits new instances of PagingData once they become invalidated by PagingSource.invalidate or calls to AsyncPagingDataDiffer.refresh or PagingDataAdapter.refresh.

To consume this stream as a LiveData or in Rx, you may use the extensions available in the paging-runtime or paging-rxjava* artifacts.

NOTE: Instances of PagingData emitted by this Flow are not re-usable and cannot be submitted multiple times. This is especially relevant for transforms such as Flow.combine, 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 Flow in a way that returns a new instance of PagingData with cached data pre-loaded.

Extension functions

PagingLiveDataKt.getLiveData

public final @NonNull LiveData<@NonNull PagingData<@NonNull Value>> PagingLiveDataKt.getLiveData(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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

NOTE: Instances of PagingData emitted by this LiveData are not re-usable and cannot be submitted multiple times. This is especially relevant because LiveData will replays 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 LiveData in a way that returns a new instance of PagingData with cached data pre-loaded.

PagingRxKt.getFlowable

public final @NonNull Flowable<@NonNull PagingData<@NonNull Value>> PagingRxKt.getFlowable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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.

PagingRxKt.getFlowable

public final @NonNull Flowable<@NonNull PagingData<@NonNull Value>> PagingRxKt.getFlowable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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.

PagingRxKt.getObservable

public final @NonNull Observable<@NonNull PagingData<@NonNull Value>> PagingRxKt.getObservable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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.

PagingRxKt.getObservable

public final @NonNull Observable<@NonNull PagingData<@NonNull Value>> PagingRxKt.getObservable(
    @NonNull Pager<@NonNull Key, @NonNull Value> receiver
)

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.