PagerAsState

Added in 3.5.0-alpha01

public final class PagerAsState


Summary

Public methods

static final @NonNull Flow<@NonNull ItemSnapshotList<@NonNull T>>
<T extends Object> asState(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull Function1<@NonNull CombinedLoadStatesUnit> onLoadError
)

Converts a Flow of PagingData into a Flow of ItemSnapshotList.

Public methods

public static final @NonNull Flow<@NonNull ItemSnapshotList<@NonNull T>> <T extends Object> asState(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull Function1<@NonNull CombinedLoadStatesUnit> onLoadError
)

Converts a Flow of PagingData into a Flow of ItemSnapshotList. An emitted ItemSnapshotList contains a snapshot of all loaded data and can be cached for repeated reads.

To reflect the latest snapshot, this flow emits a new ItemSnapshotList whenever new items are loaded or whenever loaded items are dropped (i.e. to fulfill PagingConfig.maxSize).

The flow is kept active as long as the collection scope is active. To avoid leaks, make sure to use a scope that is already managed (like a ViewModel scope) or manually cancel it when you don't need paging anymore.

To use this flow with multiple collectors, convert it to a SharedFlow with Flow operators such as stateIn() or sharedIn().

Note that this Flow remains as a cold flow and does not start any loading until collected upon.

T - the paged item type

Parameters
@NonNull Function1<@NonNull CombinedLoadStatesUnit> onLoadError

the callback invoked when any loads return androidx.paging.LoadState.Error. Provides the CombinedLoadStates containing the error. No-op by default. See Pager.retry for a recovery option.