{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ListenableFuturePagingSource

{% setvar page_path %}androidx/paging/ListenableFuturePagingSource.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

abstract class ListenableFuturePagingSource<Key : Any, Value : Any> : PagingSource

Any
   ↳ PagingSource
     ↳ 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>>(
                { 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,
                { t: IOException? -> LoadResult.Error(t!!) },
                networkExecutor
            )
            .catching(
                HttpException::class.java,
                { t: HttpException? -> LoadResult.Error(t!!) },
                networkExecutor
            )
    }

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

Summary

Public properties

Boolean

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

open Boolean

true if this PagingSource supports jumping, false otherwise.

open Boolean

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

Public constructors

<Key : Any, Value : Any> ListenableFuturePagingSource()

Public functions

open suspend PagingSource.LoadResult<Key, Value>

Loading API for PagingSource.

abstract <ERROR CLASS><PagingSource.LoadResult<Key, Value>>

Loading API for PagingSource.

Inherited functions

From class PagingSource
abstract Key?
getRefreshKey(state: PagingState<Key, Value>)

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

Unit

Signal the PagingSource to stop loading.

Unit
registerInvalidatedCallback(onInvalidatedCallback: () -> Unit)

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

Unit
unregisterInvalidatedCallback(onInvalidatedCallback: () -> Unit)

Remove a previously added invalidate callback.

Public properties

invalid

val invalidBoolean

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

jumpingSupported

open val jumpingSupportedBoolean

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

open val keyReuseSupportedBoolean

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

Public constructors

ListenableFuturePagingSource

<Key : Any, Value : Any> ListenableFuturePagingSource()

Public functions

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>): <ERROR CLASS><PagingSource.LoadResult<Key, Value>>

Loading API for PagingSource.

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