ListenableFuturePagingSource
public abstract class ListenableFuturePagingSource<Key extends Object, Value extends Object> extends PagingSource
ListenableFuture-based compatibility wrapper around PagingSource's suspending APIs.
import androidx.paging.ListenableFuturePagingSource import androidx.paging.PagingState 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 constructors |
|---|
<Key extends Object, Value extends Object> ListenableFuturePagingSource() |
Public methods |
|
|---|---|
@NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value> |
load(@NonNull PagingSource.LoadParams<@NonNull Key> params) |
abstract @NonNull ListenableFuture<@NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value>> |
loadFuture(@NonNull PagingSource.LoadParams<@NonNull Key> params)Loading API for |
Inherited methods |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
ListenableFuturePagingSource
public <Key extends Object, Value extends Object> ListenableFuturePagingSource()
Public methods
load
public @NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value> load(@NonNull PagingSource.LoadParams<@NonNull Key> params)
loadFuture
Added in 3.0.0
public abstract @NonNull ListenableFuture<@NonNull PagingSource.LoadResult<@NonNull Key, @NonNull Value>> loadFuture(@NonNull PagingSource.LoadParams<@NonNull Key> params)
Loading API for PagingSource.
Implement this method to trigger your async load (e.g. from database or network).