RxPagingSource


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


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

import androidx.paging.PagingState
import androidx.paging.rxjava2.RxPagingSource

/**
 * 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 constructors

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

Inherited methods

From androidx.paging.PagingSource
final boolean
boolean
boolean
abstract Key
final void
final void
registerInvalidatedCallback(
    @NonNull Function0<Unit> onInvalidatedCallback
)
final void
unregisterInvalidatedCallback(
    @NonNull Function0<Unit> onInvalidatedCallback
)

Public constructors

RxPagingSource

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

Public methods

load

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

loadSingle

Added in 3.0.0
public abstract @NonNull 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).