PositionalDataSource

Added in 2.0.0
Deprecated in 3.0.0

public abstract class PositionalDataSource<T extends Object> extends DataSource


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 extends Object>

Type of items being loaded by the PositionalDataSource.

Summary

Nested types

public abstract class PositionalDataSource.LoadInitialCallback<T extends Object>

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

Holder object for inputs to loadInitial.

public abstract class PositionalDataSource.LoadRangeCallback<T extends Object>

Callback for PositionalDataSource loadRange to return data.

Holder object for inputs to loadRange.

Public constructors

Public methods

static final int

Helper for computing an initial position in loadInitial when total data set size can be computed ahead of loading.

static final int
computeInitialLoadSize(
    @NonNull PositionalDataSource.LoadInitialParams params,
    int initialLoadPosition,
    int totalCount
)

Helper for computing an initial load size in loadInitial when total data set size can be computed ahead of loading.

abstract void

Load initial list data.

abstract void

Called to load a range of data from the DataSource.

final @NonNull PositionalDataSource<@NonNull V>
<V extends Object> map(@NonNull Function<@NonNull T, @NonNull V> function)

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

final @NonNull PositionalDataSource<@NonNull V>
<V extends Object> map(@NonNull Function1<@NonNull T, @NonNull V> function)

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

final @NonNull PositionalDataSource<@NonNull V>
<V extends Object> mapByPage(
    @NonNull Function<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function
)

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

final @NonNull PositionalDataSource<@NonNull V>
<V extends Object> mapByPage(
    @NonNull Function1<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function
)

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

Inherited methods

From androidx.paging.DataSource
void

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

void

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

boolean
void

Remove a previously added invalidate callback.

Public constructors

PositionalDataSource

public <T extends Object> PositionalDataSource()
Parameters
<T extends Object>

Type of items being loaded by the PositionalDataSource.

Public methods

computeInitialLoadPosition

Added in 3.3.1
Deprecated in 3.3.1
public static final int computeInitialLoadPosition(
    @NonNull PositionalDataSource.LoadInitialParams params,
    int totalCount
)

Helper for computing an initial position in loadInitial when total data set size can be computed ahead of loading.

The value computed by this function will do bounds checking, page alignment, and positioning based on initial load size requested.

Example usage in a PositionalDataSource subclass:

class ItemDataSource extends PositionalDataSource<Item> {
private int computeCount() {
// actual count code here
}

private List<Item> loadRangeInternal(int startPosition, int loadCount) {
// actual load code here
}

@Override
public void loadInitial(@NonNull LoadInitialParams params,
@NonNull LoadInitialCallback<Item> callback) {
int totalCount = computeCount();
int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);
callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
}

@Override
public void loadRange(@NonNull LoadRangeParams params,
@NonNull LoadRangeCallback<Item> callback) {
callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
}
}
Parameters
@NonNull PositionalDataSource.LoadInitialParams params

Params passed to loadInitial, including page size, and requested start / loadSize.

int totalCount

Total size of the data set.

Returns
int

Position to start loading at.

computeInitialLoadSize

Added in 3.3.1
Deprecated in 3.3.1
public static final int computeInitialLoadSize(
    @NonNull PositionalDataSource.LoadInitialParams params,
    int initialLoadPosition,
    int totalCount
)

Helper for computing an initial load size in loadInitial when total data set size can be computed ahead of loading.

This function takes the requested load size, and bounds checks it against the value returned by computeInitialLoadPosition.

Example usage in a PositionalDataSource subclass:

class ItemDataSource extends PositionalDataSource<Item> {
private int computeCount() {
// actual count code here
}

private List<Item> loadRangeInternal(int startPosition, int loadCount) {
// actual load code here
}

@Override
public void loadInitial(@NonNull LoadInitialParams params,
@NonNull LoadInitialCallback<Item> callback) {
int totalCount = computeCount();
int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);
callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
}

@Override
public void loadRange(@NonNull LoadRangeParams params,
@NonNull LoadRangeCallback<Item> callback) {
callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
}
}
Parameters
@NonNull PositionalDataSource.LoadInitialParams params

Params passed to loadInitial, including page size, and requested start / loadSize.

int initialLoadPosition

Value returned by computeInitialLoadPosition

int totalCount

Total size of the data set.

Returns
int

Number of items to load.

loadInitial

Added in 2.0.0
Deprecated in 3.0.0
@WorkerThread
public abstract void loadInitial(
    @NonNull PositionalDataSource.LoadInitialParams params,
    @NonNull PositionalDataSource.LoadInitialCallback<@NonNull T> callback
)

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
@NonNull PositionalDataSource.LoadInitialParams params

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

@NonNull PositionalDataSource.LoadInitialCallback<@NonNull T> callback

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

loadRange

Added in 2.0.0
Deprecated in 3.0.0
@WorkerThread
public abstract void loadRange(
    @NonNull PositionalDataSource.LoadRangeParams params,
    @NonNull PositionalDataSource.LoadRangeCallback<@NonNull T> callback
)

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
@NonNull PositionalDataSource.LoadRangeParams params

Parameters for load, including start position and load size.

@NonNull PositionalDataSource.LoadRangeCallback<@NonNull T> callback

Callback that receives loaded data.

map

Added in 2.0.0
Deprecated in 3.0.0
public final @NonNull PositionalDataSource<@NonNull V> <V extends Object> map(@NonNull Function<@NonNull T, @NonNull V> function)

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

Same as mapByPage, but operates on individual items.

Parameters
@NonNull Function<@NonNull T, @NonNull V> function

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

Returns
@NonNull PositionalDataSource<@NonNull V>

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

map

Added in 3.0.0
Deprecated in 3.0.0
public final @NonNull PositionalDataSource<@NonNull V> <V extends Object> map(@NonNull Function1<@NonNull T, @NonNull V> function)

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
@NonNull Function1<@NonNull T, @NonNull V> function

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

Returns
@NonNull PositionalDataSource<@NonNull V>

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

See also
mapByPage
map

mapByPage

Added in 2.0.0
Deprecated in 3.0.0
public final @NonNull PositionalDataSource<@NonNull V> <V extends Object> mapByPage(
    @NonNull Function<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function
)

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

Same as map, but allows for batch conversions.

Parameters
@NonNull Function<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function

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

Returns
@NonNull PositionalDataSource<@NonNull V>

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

See also
map
map
mapByPage

mapByPage

public final @NonNull PositionalDataSource<@NonNull V> <V extends Object> mapByPage(
    @NonNull Function1<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function
)

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
@NonNull Function1<@NonNull List<@NonNull T>, @NonNull List<@NonNull V>> function

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

Returns
@NonNull PositionalDataSource<@NonNull V>

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

See also
map
map
mapByPage