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

PositionalDataSource

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

abstract class PositionalDataSource<T : Any> : DataSource

Any
   ↳ DataSource
     ↳ PositionalDataSource

Position-based data loader for a fixed-size, countable data set, supporting fixed-size loads at arbitrary page positions.

Extend PositionalDataSource if you can load pages of a requested size at arbitrary positions, and provide a fixed item count. If your data source can't support loading arbitrary requested page sizes (e.g. when network page size constraints are only known at runtime), either use PageKeyedDataSource or ItemKeyedDataSource, or pass the initial result with the two parameter LoadInitialCallback.onResult.

Room can generate a Factory of PositionalDataSources for you:

@Dao
interface UserDao {
@Query("SELECT * FROM user ORDER BY age DESC")
public abstract DataSource.Factory<Integer, User> loadUsersByAgeDesc();
}
Parameters
<T : Any>

Type of items being loaded by the PositionalDataSource.

Summary

Nested types

PositionalDataSource.LoadInitialCallback

Callback for loadInitial to return data, position, and count.

PositionalDataSource.LoadInitialParams

Holder object for inputs to loadInitial.

PositionalDataSource.LoadRangeCallback

Callback for PositionalDataSource loadRange to return data.

PositionalDataSource.LoadRangeParams

Holder object for inputs to loadRange.

Public properties

open Boolean

Public constructors

Public functions

abstract @<ERROR CLASS> Unit

Load initial list data.

abstract @<ERROR CLASS> Unit

Called to load a range of data from the DataSource.

final PositionalDataSource<V>
<V : Any> map(function: <ERROR CLASS><T, V>)

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

final PositionalDataSource<V>
<V : Any> map(function: (T) -> V)

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

final PositionalDataSource<V>
<V : Any> mapByPage(function: <ERROR CLASS><List<T>, List<V>>)

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

final PositionalDataSource<V>
<V : Any> mapByPage(function: (List<T>) -> List<V>)

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

Inherited functions

From class DataSource
open @<ERROR CLASS> Unit

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

open @<ERROR CLASS> Unit

Signal the data source to stop loading, and notify its callback.

open @<ERROR CLASS> Unit

Remove a previously added invalidate callback.

Public properties

isInvalid

open val isInvalidBoolean
Returns
Boolean

true if the data source is invalid, and can no longer be queried for data.

Public constructors

PositionalDataSource

<T : Any> PositionalDataSource()
Parameters
<T : Any>

Type of items being loaded by the PositionalDataSource.

Public functions

loadInitial

@<ERROR CLASS>
abstract fun loadInitial(
    params: PositionalDataSource.LoadInitialParams,
    callback: PositionalDataSource.LoadInitialCallback<T>
): @<ERROR CLASS> Unit

Load initial list data.

This method is called to load the initial page(s) from the DataSource.

LoadResult list must be a multiple of pageSize to enable efficient tiling.

Parameters
params: PositionalDataSource.LoadInitialParams

Parameters for initial load, including requested start position, load size, and page size.

callback: PositionalDataSource.LoadInitialCallback<T>

Callback that receives initial load data, including position and total data set size.

loadRange

@<ERROR CLASS>
abstract fun loadRange(
    params: PositionalDataSource.LoadRangeParams,
    callback: PositionalDataSource.LoadRangeCallback<T>
): @<ERROR CLASS> Unit

Called to load a range of data from the DataSource.

This method is called to load additional pages from the DataSource after the LoadInitialCallback passed to dispatchLoadInitial has initialized a PagedList.

Unlike loadInitial, this method must return the number of items requested, at the position requested.

Parameters
params: PositionalDataSource.LoadRangeParams

Parameters for load, including start position and load size.

callback: PositionalDataSource.LoadRangeCallback<T>

Callback that receives loaded data.

map

final fun <V : Any> map(function: <ERROR CLASS><T, V>): PositionalDataSource<V>

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

Same as mapByPage, but operates on individual items.

Parameters
function: <ERROR CLASS><T, V>

Function that runs on each loaded item, returning items of a potentially new type.

Returns
PositionalDataSource<V>

A new DataSource, which transforms items using the given function.

See also
mapByPage
map
mapByPage

map

final fun <V : Any> map(function: (T) -> V): PositionalDataSource<V>

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

Same as mapByPage, but operates on individual items.

Parameters
function: (T) -> V

Function that runs on each loaded item, returning items of a potentially new type.

Returns
PositionalDataSource<V>

A new DataSource, which transforms items using the given function.

See also
mapByPage
map
mapByPage

mapByPage

final fun <V : Any> mapByPage(function: <ERROR CLASS><List<T>, List<V>>): PositionalDataSource<V>

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

Same as map, but allows for batch conversions.

Parameters
function: <ERROR CLASS><List<T>, List<V>>

Function that runs on each loaded page, returning items of a potentially new type.

Returns
PositionalDataSource<V>

A new DataSource, which transforms items using the given function.

See also
map
map
mapByPage

mapByPage

final fun <V : Any> mapByPage(function: (List<T>) -> List<V>): PositionalDataSource<V>

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

Same as map, but allows for batch conversions.

Parameters
function: (List<T>) -> List<V>

Function that runs on each loaded page, returning items of a potentially new type.

Returns
PositionalDataSource<V>

A new DataSource, which transforms items using the given function.

See also
map
map
mapByPage