# PageKeyedDataSource

Summary:[Nested Classes](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#nestedclasses)\|[Ctors](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#pubctors)\|[Methods](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#inhmethods)  

# PageKeyedDataSource

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.paging.PageKeyedDataSource](https://developer.android.com/reference/androidx/paging/PageKeyedDataSource)instead.

`
public


abstract
class
PageKeyedDataSource
`  
`


``

extends `[DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)`<Key, Value>


``


`

|---|---|-------------------------------------------------------|
| java.lang.Object                                            |||
| ↳ | [android.arch.paging.DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)\<Key, Value\> ||
|   | ↳ | android.arch.paging.PageKeyedDataSource\<Key, Value\> |

<br />

*** ** * ** ***

Incremental data loader for page-keyed content, where requests return keys for next/previous pages.

Implement a DataSource using PageKeyedDataSource if you need to use data from page`N - 1`to load page`N`. This is common, for example, in network APIs that include a next/previous link or key with each page load.

The`InMemoryByPageRepository`in the[PagingWithNetworkSample](https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample/README.md)shows how to implement a network PageKeyedDataSource using[Retrofit](https://square.github.io/retrofit/), while handling swipe-to-refresh, network errors, and retry.

## Summary

|                                                                                                                                                                                                                                                                                                                                                         ### Nested classes                                                                                                                                                                                                                                                                                                                                                          ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [PageKeyedDataSource.LoadCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback)`<Key, Value>` Callback for PageKeyedDataSource[loadBefore(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadBefore(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>))and[loadAfter(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadAfter(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>))to return data. |
| ` class` | [PageKeyedDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback)`<Key, Value>` Callback for[loadInitial(LoadInitialParams, LoadInitialCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadInitial(android.arch.paging.PageKeyedDataSource.LoadInitialParams<Key>, android.arch.paging.PageKeyedDataSource.LoadInitialCallback<Key, Value>))to return data and, optionally, position/count information.                                                                                                                                                                                           |
| ` class` | [PageKeyedDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams)`<Key>` Holder object for inputs to[loadInitial(LoadInitialParams, LoadInitialCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadInitial(android.arch.paging.PageKeyedDataSource.LoadInitialParams<Key>, android.arch.paging.PageKeyedDataSource.LoadInitialCallback<Key, Value>)).                                                                                                                                                                                                                                                 |
| ` class` | [PageKeyedDataSource.LoadParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams)`<Key>` Holder object for inputs to[loadBefore(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadBefore(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>))and[loadAfter(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadAfter(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>)).                               |

|                                                         ### Public constructors                                                         ||
|--------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#PageKeyedDataSource())`() ` |

|                                                                                                                                                                                                                                                                                                                                                                                            ### Public methods                                                                                                                                                                                                                                                                                                                                                                                             ||
|-------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract void`                                                                                                                          | ` `[loadAfter](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadAfter(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>))`(`[LoadParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams)`<Key> params, `[LoadCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback)`<Key, Value> callback) ` Append page with the key specified by[LoadParams.key](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams#key).    |
| ` abstract void`                                                                                                                          | ` `[loadBefore](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadBefore(android.arch.paging.PageKeyedDataSource.LoadParams<Key>, android.arch.paging.PageKeyedDataSource.LoadCallback<Key, Value>))`(`[LoadParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams)`<Key> params, `[LoadCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback)`<Key, Value> callback) ` Prepend page with the key specified by[LoadParams.key](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams#key). |
| ` abstract void`                                                                                                                          | ` `[loadInitial](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#loadInitial(android.arch.paging.PageKeyedDataSource.LoadInitialParams<Key>, android.arch.paging.PageKeyedDataSource.LoadInitialCallback<Key, Value>))`(`[LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams)`<Key> params, `[LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback)`<Key, Value> callback) ` Load initial data.                                                                                          |
| ` final <ToValue> `[PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)`<Key, ToValue>` | ` `[map](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#map(android.arch.core.util.Function<Value, ToValue>))`(`[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<Value, ToValue> function) ` Applies the given function to each value emitted by the DataSource.                                                                                                                                                                                                                                                                                                                          |
| ` final <ToValue> `[PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)`<Key, ToValue>` | ` `[mapByPage](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource#mapByPage(android.arch.core.util.Function<java.util.List<Value>, java.util.List<ToValue>>))`(`[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<List<Value>, List<ToValue>> function) ` Applies the given function to each value emitted by the DataSource.                                                                                                                                                                                                                                                                  |

| ### Inherited methods |
|-----------------------|---|
| From class` `[android.arch.paging.DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)` ` |----------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void`                                                                                                                    | ` `[addInvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource#addInvalidatedCallback(android.arch.paging.DataSource.InvalidatedCallback))`(`[DataSource.InvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource.InvalidatedCallback)` onInvalidatedCallback) ` Add a callback to invoke when the DataSource is first invalidated. | | ` void`                                                                                                                    | ` `[invalidate](https://developer.android.com/reference/android/arch/paging/DataSource#invalidate())`() ` Signal the data source to stop loading, and notify its callback.                                                                                                                                                                                                                                 | | ` boolean`                                                                                                                 | ` `[isInvalid](https://developer.android.com/reference/android/arch/paging/DataSource#isInvalid())`() ` Returns true if the data source is invalid, and can no longer be queried for data.                                                                                                                                                                                                                 | | ` abstract <ToValue> `[DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)`<Key, ToValue>` | ` `[map](https://developer.android.com/reference/android/arch/paging/DataSource#map(android.arch.core.util.Function<Value, ToValue>))`(`[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<Value, ToValue> function) ` Applies the given function to each value emitted by the DataSource.                                                                               | | ` abstract <ToValue> `[DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)`<Key, ToValue>` | ` `[mapByPage](https://developer.android.com/reference/android/arch/paging/DataSource#mapByPage(android.arch.core.util.Function<java.util.List<Value>, java.util.List<ToValue>>))`(`[Function](https://developer.android.com/reference/android/arch/core/util/Function)`<List<Value>, List<ToValue>> function) ` Applies the given function to each value emitted by the DataSource.                       | | ` void`                                                                                                                    | ` `[removeInvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource#removeInvalidatedCallback(android.arch.paging.DataSource.InvalidatedCallback))`(`[DataSource.InvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource.InvalidatedCallback)` onInvalidatedCallback) ` Remove a previously added invalidate callback.               | ||
| From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### PageKeyedDataSource

```
PageKeyedDataSource ()
```

<br />

<br />

## Public methods

### loadAfter

```
void loadAfter (LoadParams<Key> params, 
                LoadCallback<Key, Value> callback)
```

Append page with the key specified by[LoadParams.key](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams#key).

It's valid to return a different list size than the page size if it's easier, e.g. if your backend defines page sizes. It is generally safer to increase the number loaded than reduce.

Data may be passed synchronously during the load method, or deferred and called at a later time. Further loads going down will be blocked until the callback is called.

If data cannot be loaded (for example, if the request is invalid, or the data would be stale and inconsistent, it is valid to call[invalidate()](https://developer.android.com/reference/android/arch/paging/DataSource#invalidate())to invalidate the data source, and prevent further loading.

<br />

|                                                       Parameters                                                       ||
|------------|------------------------------------------------------------------------------------------------------------|
| `params`   | `LoadParams`: Parameters for the load, including the key for the new page, and requested load size. <br /> |
| `callback` | `LoadCallback`: Callback that receives loaded data. <br />                                                 |

### loadBefore

```
void loadBefore (LoadParams<Key> params, 
                LoadCallback<Key, Value> callback)
```

Prepend page with the key specified by[LoadParams.key](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams#key).

It's valid to return a different list size than the page size if it's easier, e.g. if your backend defines page sizes. It is generally safer to increase the number loaded than reduce.

Data may be passed synchronously during the load method, or deferred and called at a later time. Further loads going down will be blocked until the callback is called.

If data cannot be loaded (for example, if the request is invalid, or the data would be stale and inconsistent, it is valid to call[invalidate()](https://developer.android.com/reference/android/arch/paging/DataSource#invalidate())to invalidate the data source, and prevent further loading.

<br />

|                                                       Parameters                                                       ||
|------------|------------------------------------------------------------------------------------------------------------|
| `params`   | `LoadParams`: Parameters for the load, including the key for the new page, and requested load size. <br /> |
| `callback` | `LoadCallback`: Callback that receives loaded data. <br />                                                 |

### loadInitial

```
void loadInitial (LoadInitialParams<Key> params, 
                LoadInitialCallback<Key, Value> callback)
```

Load initial data.

This method is called first to initialize a PagedList with data. If it's possible to count the items that can be loaded by the DataSource, it's recommended to pass the loaded data to the callback via the three-parameter[onResult(List, int, int, Object, Object)](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback#onResult(java.util.List<Value>, int, int, Key, Key)). This enables PagedLists presenting data from this source to display placeholders to represent unloaded items.

[requestedLoadSize](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams#requestedLoadSize)is a hint, not a requirement, so it may be may be altered or ignored.

<br />

|                                             Parameters                                              ||
|------------|-----------------------------------------------------------------------------------------|
| `params`   | `LoadInitialParams`: Parameters for initial load, including requested load size. <br /> |
| `callback` | `LoadInitialCallback`: Callback that receives initial load data. <br />                 |

### map

```
PageKeyedDataSource<Key, ToValue> map (Function<Value, ToValue> function)
```

Applies the given function to each value emitted by the DataSource.

Same as[mapByPage(Function)](https://developer.android.com/reference/android/arch/paging/DataSource#mapByPage(android.arch.core.util.Function<java.util.List<Value>, java.util.List<ToValue>>)), but operates on individual items.

<br />

|                                                    Parameters                                                     ||
|------------|-------------------------------------------------------------------------------------------------------|
| `function` | `Function`: Function that runs on each loaded item, returning items of a potentially new type. <br /> |

|                                                                                              Returns                                                                                              ||
|------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| [PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)`<Key, ToValue>` | A new DataSource, which transforms items using the given function. <br /> |

### mapByPage

```
PageKeyedDataSource<Key, ToValue> mapByPage (Function<List<Value>, List<ToValue>> function)
```

Applies the given function to each value emitted by the DataSource.

Same as[map(Function)](https://developer.android.com/reference/android/arch/paging/DataSource#map(android.arch.core.util.Function<Value, ToValue>)), but allows for batch conversions.

<br />

|                                                    Parameters                                                     ||
|------------|-------------------------------------------------------------------------------------------------------|
| `function` | `Function`: Function that runs on each loaded page, returning items of a potentially new type. <br /> |

|                                                                                              Returns                                                                                              ||
|------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| [PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)`<Key, ToValue>` | A new DataSource, which transforms items using the given function. <br /> |

-

  ## Interfaces

  - [DataSource.InvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource.InvalidatedCallback)
-

  ## Classes

  - [AsyncPagedListDiffer](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer)
  - [DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)
  - [DataSource.Factory](https://developer.android.com/reference/android/arch/paging/DataSource.Factory)
  - [ItemKeyedDataSource](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource)
  - [ItemKeyedDataSource.LoadCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadCallback)
  - [ItemKeyedDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialCallback)
  - [ItemKeyedDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialParams)
  - [ItemKeyedDataSource.LoadParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams)
  - [LivePagedListBuilder](https://developer.android.com/reference/android/arch/paging/LivePagedListBuilder)
  - [PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)
  - [PagedList.BoundaryCallback](https://developer.android.com/reference/android/arch/paging/PagedList.BoundaryCallback)
  - [PagedList.Builder](https://developer.android.com/reference/android/arch/paging/PagedList.Builder)
  - [PagedList.Callback](https://developer.android.com/reference/android/arch/paging/PagedList.Callback)
  - [PagedList.Config](https://developer.android.com/reference/android/arch/paging/PagedList.Config)
  - [PagedList.Config.Builder](https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder)
  - [PagedListAdapter](https://developer.android.com/reference/android/arch/paging/PagedListAdapter)
  - [PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)
  - [PageKeyedDataSource.LoadCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback)
  - [PageKeyedDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback)
  - [PageKeyedDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams)
  - [PageKeyedDataSource.LoadParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams)
  - [PositionalDataSource](https://developer.android.com/reference/android/arch/paging/PositionalDataSource)
  - [PositionalDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadInitialCallback)
  - [PositionalDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadInitialParams)
  - [PositionalDataSource.LoadRangeCallback](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadRangeCallback)
  - [PositionalDataSource.LoadRangeParams](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadRangeParams)
  - [RxPagedListBuilder](https://developer.android.com/reference/android/arch/paging/RxPagedListBuilder)