Google is committed to advancing racial equity for Black communities. See how.

PositionalDataSource

abstract class PositionalDataSource<T : Any> : DataSource<Int, T>
kotlin.Any
   ↳ androidx.paging.DataSource<kotlin.Int, T>
   ↳ androidx.paging.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();
}

Summary

Nested classes

abstract

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

open

Holder object for inputs to loadInitial.

abstract

Callback for PositionalDataSource loadRange to return data.

open

Holder object for inputs to loadRange.

Public constructors

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

Public methods

abstract Unit

Load initial list data.

abstract Unit

Called to load a range of data from the DataSource.

PositionalDataSource<V>
map(function: Function<T, V>)

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

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

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

PositionalDataSource<V>
mapByPage(function: Function<List<T>, List<V>>)

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

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

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

Inherited functions

Inherited properties

Public constructors

<init>

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 Type of items being loaded by the PositionalDataSource.

Public methods

loadInitial

@WorkerThread abstract fun loadInitial(
    params: PositionalDataSource.LoadInitialParams,
    callback: PositionalDataSource.LoadInitialCallback<T>
): 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

@WorkerThread abstract fun loadRange(
    params: PositionalDataSource.LoadRangeParams,
    callback: PositionalDataSource.LoadRangeCallback<T>
): 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

fun <V : Any> map(function: 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: Function<T, V> Function that runs on each loaded item, returning items of a potentially new type.
ToValue Type of items produced by the new DataSource, from the passed function.
Return
A new DataSource, which transforms items using the given function.

map

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

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

An overload of map that accepts a kotlin function type.

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.
ToValue Type of items produced by the new DataSource, from the passed function.
Return
A new DataSource, which transforms items using the given function.

mapByPage

fun <V : Any> mapByPage(function: 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: Function<List<T>, List<V>> Function that runs on each loaded page, returning items of a potentially new type.
ToValue Type of items produced by the new DataSource, from the passed function.
Return
A new DataSource, which transforms items using the given function.

mapByPage

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

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

An overload of mapByPage that accepts a kotlin function type.

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.
ToValue Type of items produced by the new DataSource, from the passed function.
Return
A new DataSource, which transforms items using the given function.