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 void

Loads a page at the end of current loaded data.

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.

final void

Loads a page at the start of current loaded data.

final void

Refresh all currently loaded data.

final void
refresh(@NonNull Value item)

Refresh based on a given loaded item.

final void

Retry most recent load that returned PagingSource.LoadResult.Error.

Extension functions

final @NonNull LiveData<@NonNull PagingData<@NonNull Value>>
PagingLiveData.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>>
PagingRx.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>>
PagingRx.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>>
PagingRx.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>>
PagingRx.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

append

Added in 3.5.0-alpha01
public final void append()

Loads a page at the end of current loaded data. Provides a way to manually trigger appends without scrolling.

This function is no-op if there are no more data to be loaded from the datasource (i.e. append is LoadState.NotLoading.Complete)

Appends that are triggered by scrolling (i.e. if user scrolls to end of the list and triggers PagingConfig.prefetchDistance) takes precedence over this append. This ensures that items that are currently accessed gets loaded in first.

No-op if preceding append returned LoadState.Error. To recover from error, use retry

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.

prepend

Added in 3.5.0-alpha01
public final void prepend()

Loads a page at the start of current loaded data. Provides a way to manually trigger prepends without scrolling.

This function is no-op if there are no more data to be loaded from the datasource (i.e. prepend is LoadState.NotLoading.Complete)

Prepends that are triggered by scrolling (i.e. if user scrolls to start of the list and triggers PagingConfig.prefetchDistance) takes precedence over this prepend. This ensures that items that are currently accessed gets loaded in first.

No-op if preceding prepend returned LoadState.Error. To recover from error, use retry

refresh

Added in 3.5.0-alpha01
public final void refresh()

Refresh all currently loaded data.

The refresh key is the key that was used to load the current first loaded page, and the loadSize is the size of current loaded data.

Note that this is a best-effort attempt to reload all current data. Actual loaded data may differ based on how PagingSource.load handles the same key for different LoadType. For example the current first page might have been a prepended page, and PagingSource.load could handle the same key in different ways for LoadType.PREPEND versus LoadType.REFRESH

refresh

Added in 3.5.0-alpha01
public final void refresh(@NonNull Value item)

Refresh based on a given loaded item.

The refresh key is the key that was originally used to the load the page that contains item, and the loadSize is PagingConfig.initialLoadSize.

Note that this is a best-effort attempt to reload around the given loaded item. The requested item is expected to be in the first page of reloaded items but will likely not be the very first reloaded item. For example if two pages are currently loaded with loadKeys MyKey1 and MyKey2 respectively:

  • pg1: MyKey1 = items 0, 1, 2, 3

  • pg2: MyKey2 = items 4, 5, 6, 7 If this method were called with item 5 - refresh(5) - then the refreshKey will be MyKey2, which will theoretically refresh starting at (item 4) until (item4 + initialLoadSize).

retry

Added in 3.5.0-alpha01
public final void retry()

Retry most recent load that returned PagingSource.LoadResult.Error. No-op if there were no errors.

Applicable to all LoadTypes. The retry is not guaranteed to succeed as the cause of the initial may persist on retries.

Extension functions

PagingLiveData.getLiveData

public final @NonNull LiveData<@NonNull PagingData<@NonNull Value>> PagingLiveData.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.

PagingRx.getFlowable

public final @NonNull Flowable<@NonNull PagingData<@NonNull Value>> PagingRx.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.

PagingRx.getFlowable

public final @NonNull Flowable<@NonNull PagingData<@NonNull Value>> PagingRx.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.

PagingRx.getObservable

public final @NonNull Observable<@NonNull PagingData<@NonNull Value>> PagingRx.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.

PagingRx.getObservable

public final @NonNull Observable<@NonNull PagingData<@NonNull Value>> PagingRx.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.