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

ListenableFuturePagingSource

abstract class ListenableFuturePagingSource<Key : Any, Value : Any> : PagingSource<Key, Value>
kotlin.Any
   ↳ androidx.paging.PagingSource<Key, Value>
   ↳ androidx.paging.ListenableFuturePagingSource

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


class MyListenableFuturePagingSource(
    val myBackend: GuavaBackendService,
    val searchTerm: String
) : ListenableFuturePagingSource<String, Item>() {
    override fun loadFuture(
        params: LoadParams<String>
    ): ListenableFuture<LoadResult<String, Item>> {
        return myBackend.searchUsers(
            searchTerm = searchTerm,
            pageKey = params.key
        )
            .transform<LoadResult<String, Item>>(
                Function { response ->
                    LoadResult.Page(
                        data = response!!.items,
                        prevKey = response.prev,
                        nextKey = response.next
                    )
                },
                networkExecutor
            )
            // 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.
            .catching(
                IOException::class.java,
                Function { t: IOException? ->
                    LoadResult.Error<String, Item>(t!!)
                },
                networkExecutor
            )
            .catching(
                HttpException::class.java,
                Function { t: HttpException? ->
                    LoadResult.Error<String, Item>(t!!)
                },
                networkExecutor
            )
    }
}

Summary

Public constructors

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

Public methods
open suspend PagingSource.LoadResult<Key, Value>

Loading API for PagingSource.

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

Loading API for PagingSource.

Inherited functions
Inherited properties

Public constructors

<init>

ListenableFuturePagingSource()

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


class MyListenableFuturePagingSource(
    val myBackend: GuavaBackendService,
    val searchTerm: String
) : ListenableFuturePagingSource<String, Item>() {
    override fun loadFuture(
        params: LoadParams<String>
    ): ListenableFuture<LoadResult<String, Item>> {
        return myBackend.searchUsers(
            searchTerm = searchTerm,
            pageKey = params.key
        )
            .transform<LoadResult<String, Item>>(
                Function { response ->
                    LoadResult.Page(
                        data = response!!.items,
                        prevKey = response.prev,
                        nextKey = response.next
                    )
                },
                networkExecutor
            )
            // 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.
            .catching(
                IOException::class.java,
                Function { t: IOException? ->
                    LoadResult.Error<String, Item>(t!!)
                },
                networkExecutor
            )
            .catching(
                HttpException::class.java,
                Function { t: HttpException? ->
                    LoadResult.Error<String, Item>(t!!)
                },
                networkExecutor
            )
    }
}

Public methods

load

open 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).

loadFuture

abstract fun loadFuture(params: PagingSource.LoadParams<Key>): ListenableFuture<PagingSource.LoadResult<Key, Value>>

Loading API for PagingSource.

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