Google is committed to advancing racial equity for Black communities. See how.

RxPagingSource

abstract class RxPagingSource<Key : Any, Value : Any> : PagingSource<Key, Value>
kotlin.Any
   ↳ androidx.paging.PagingSource<Key, Value>
   ↳ androidx.paging.rxjava3.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
                }
            }
    }
}

Summary

Public constructors

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

Public methods
suspend PagingSource.LoadResult<Key, Value>

Loading API for PagingSource.

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

Loading API for PagingSource.

Inherited functions
Inherited properties

Public constructors

<init>

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
                }
            }
    }
}

Public methods

load

suspend fun load(params: PagingSource.LoadParams<Key>): PagingSource.LoadResult<Key, Value>

Loading API for PagingSource.

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

loadSingle

abstract fun loadSingle(params: PagingSource.LoadParams<Key>): Single<PagingSource.LoadResult<Key, Value>>

Loading API for PagingSource.

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