RxPagingSource

public abstract class RxPagingSource<Key extends Object, Value extends Object> extends PagingSource

Object
   ↳ PagingSource
     ↳ RxPagingSource

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

/**
 * Sample RxPagingSource which loads `Item`s from network requests via Retrofit to a backend
 * service, which uses String tokens to load pages (each response has a next/previous token).
 */
class MyPagingSource(
    val myBackend: RxBackendService,
    val searchTerm: String
) : RxPagingSource<String, Item>() {
    override fun loadSingle(params: LoadParams<String>): Single<LoadResult<String, Item>> {
        return myBackend
            // Single-based network load
            .searchUsers(searchTerm, params.key)
            .subscribeOn(Schedulers.io())
            .map<LoadResult<String, Item>> { result ->
                LoadResult.Page(
                    data = result.items,
                    prevKey = result.prev,
                    nextKey = result.next
                )
            }
            .onErrorReturn { e ->
                when (e) {
                    // Retrofit calls that return the body type throw either IOException for
                    // network failures, or HttpException for any non-2xx HTTP status codes.
                    // This code reports all errors to the UI, but you can inspect/wrap the
                    // exceptions to provide more context.
                    is IOException -> LoadResult.Error(e)
                    is HttpException -> LoadResult.Error(e)
                    else -> throw e
                }
            }
    }

    override fun getRefreshKey(state: PagingState<String, Item>): String? {
        return state.anchorPosition?.let { state.closestItemToPosition(it)?.id }
    }
}

Summary

Public fields

final boolean

Whether this PagingSource has been invalidated, which should happen when the data this PagingSource represents changes since it was first instantiated.

boolean

true if this PagingSource supports jumping, false otherwise.

boolean

true if this PagingSource expects to re-use keys to load distinct pages without a call to invalidate, false otherwise.

Public constructors

<Key extends Object, Value extends Object> RxPagingSource()

Public methods

final @NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value>

Loading API for PagingSource.

abstract @NonNull Single<@NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value>>

Loading API for PagingSource.

Inherited methods

From class PagingSource
abstract @Nullable Key

Provide a Key used for the initial load for the next PagingSource due to invalidation of this PagingSource.

final void

Signal the PagingSource to stop loading.

final void
registerInvalidatedCallback(
    @NonNull Function0<Unit> onInvalidatedCallback
)

Add a callback to invoke when the PagingSource is first invalidated.

final void
unregisterInvalidatedCallback(
    @NonNull Function0<Unit> onInvalidatedCallback
)

Remove a previously added invalidate callback.

Public fields

invalid

@NonNull
public final boolean invalid

Whether this PagingSource has been invalidated, which should happen when the data this PagingSource represents changes since it was first instantiated.

jumpingSupported

@NonNull
public boolean jumpingSupported

true if this PagingSource supports jumping, false otherwise.

Override this to true if pseudo-fast scrolling via jumps is supported.

A jump occurs when a RecyclerView scrolls through a number of placeholders defined by PagingConfig.jumpThreshold and triggers a load with LoadType.

PagingSources that support jumps should override getRefreshKey to return a Key that would load data fulfilling the viewport given a user's current PagingState.anchorPosition.

See also
jumpThreshold

keyReuseSupported

@NonNull
public boolean keyReuseSupported

true if this PagingSource expects to re-use keys to load distinct pages without a call to invalidate, false otherwise.

Public constructors

RxPagingSource

public final <Key extends Object, Value extends Object> RxPagingSource()

Public methods

load

@NonNull
public final PagingSource.LoadResult<@NonNull Key, @NonNull Value> load(@NonNull PagingSource.LoadParams<@NonNull Key> params)

Loading API for PagingSource.

Implement this method to trigger your async load (e.g. from database or network).

loadSingle

@NonNull
public abstract Single<@NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value>> loadSingle(@NonNull PagingSource.LoadParams<@NonNull Key> params)

Loading API for PagingSource.

Implement this method to trigger your async load (e.g. from database or network).