# ItemKeyedDataSource

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

# ItemKeyedDataSource

| 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.ItemKeyedDataSource](https://developer.android.com/reference/androidx/paging/ItemKeyedDataSource)instead.

`
public


abstract
class
ItemKeyedDataSource
`  
`


``

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.ItemKeyedDataSource\<Key, Value\> |

<br />

*** ** * ** ***

Incremental data loader for paging keyed content, where loaded content uses previously loaded items as input to future loads.

Implement a DataSource using ItemKeyedDataSource if you need to use data from item`N - 1`to load item`N`. This is common, for example, in sorted database queries where attributes of the item such just before the next query define how to execute it.

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

## Summary

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

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

|                                                                                                                                                                                                                                                                                                                                                                                         ### Public methods                                                                                                                                                                                                                                                                                                                                                                                          ||
|-------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract Key`                                                                                                                           | ` `[getKey](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#getKey(Value))`(Value item) ` Return a key associated with the given item.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ` abstract void`                                                                                                                          | ` `[loadAfter](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#loadAfter(android.arch.paging.ItemKeyedDataSource.LoadParams<Key>, android.arch.paging.ItemKeyedDataSource.LoadCallback<Value>))`(`[LoadParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams)`<Key> params, `[LoadCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadCallback)`<Value> callback) ` Load list data after the key specified in[LoadParams.key](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams#key).    |
| ` abstract void`                                                                                                                          | ` `[loadBefore](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#loadBefore(android.arch.paging.ItemKeyedDataSource.LoadParams<Key>, android.arch.paging.ItemKeyedDataSource.LoadCallback<Value>))`(`[LoadParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams)`<Key> params, `[LoadCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadCallback)`<Value> callback) ` Load list data before the key specified in[LoadParams.key](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams#key). |
| ` abstract void`                                                                                                                          | ` `[loadInitial](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#loadInitial(android.arch.paging.ItemKeyedDataSource.LoadInitialParams<Key>, android.arch.paging.ItemKeyedDataSource.LoadInitialCallback<Value>))`(`[LoadInitialParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialParams)`<Key> params, `[LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialCallback)`<Value> callback) ` Load initial data.                                                                                              |
| ` final <ToValue> `[ItemKeyedDataSource](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource)`<Key, ToValue>` | ` `[map](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#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> `[ItemKeyedDataSource](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource)`<Key, ToValue>` | ` `[mapByPage](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#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

### ItemKeyedDataSource

```
ItemKeyedDataSource ()
```

<br />

<br />

## Public methods

### getKey

```
Key getKey (Value item)
```

Return a key associated with the given item.

If your ItemKeyedDataSource is loading from a source that is sorted and loaded by a unique integer ID, you would return`item.getID()`here. This key can then be passed to[loadBefore(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#loadBefore(android.arch.paging.ItemKeyedDataSource.LoadParams<Key>, android.arch.paging.ItemKeyedDataSource.LoadCallback<Value>))or[loadAfter(LoadParams, LoadCallback)](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource#loadAfter(android.arch.paging.ItemKeyedDataSource.LoadParams<Key>, android.arch.paging.ItemKeyedDataSource.LoadCallback<Value>))to load additional items adjacent to the item passed to this function.

If your key is more complex, such as when you're sorting by name, then resolving collisions with integer ID, you'll need to return both. In such a case you would use a wrapper class, such as`Pair<String, Integer>`or, in Kotlin,`data class Key(val name: String, val id: Int)`

<br />

|                    Parameters                     ||
|--------|-------------------------------------------|
| `item` | `Value`: Item to get the key from. <br /> |

|                    Returns                    ||
|-------|----------------------------------------|
| `Key` | Key associated with given item. <br /> |

### loadAfter

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

Load list data after the key specified in[LoadParams.key](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.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 loadAfter 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 to load after, and requested size. <br /> |
| `callback` | `LoadCallback`: Callback that receives loaded data. <br />                                         |

### loadBefore

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

Load list data before the key specified in[LoadParams.key](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.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.

<br />

**Note:**Data returned will be prepended just before the key passed, so if you vary size, ensure that the last item is adjacent to the passed key.

Data may be passed synchronously during the loadBefore method, or deferred and called at a later time. Further loads going up 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 to load before, and requested size. <br /> |
| `callback` | `LoadCallback`: Callback that receives loaded data. <br />                                          |

### loadInitial

```
void loadInitial (LoadInitialParams<Key> params, 
                LoadInitialCallback<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)](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialCallback#onResult(java.util.List<Value>, int, int)). This enables PagedLists presenting data from this source to display placeholders to represent unloaded items.

[requestedInitialKey](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialParams#requestedInitialKey)and[requestedLoadSize](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialParams#requestedLoadSize)are hints, not requirements, so they may be altered or ignored. Note that ignoring the`requestedInitialKey`can prevent subsequent PagedList/DataSource pairs from initializing at the same location. If your data source never invalidates (for example, loading from the network without the network ever signalling that old data must be reloaded), it's fine to ignore the`initialLoadKey`and always start from the beginning of the data set.

<br />

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

### map

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

### mapByPage

```
ItemKeyedDataSource<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                                                                                              ||
|------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| [ItemKeyedDataSource](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource)`<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)