PositionalDataSource
-
android
abstract class PositionalDataSource<T : Any> : 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 : Any> |
Type of items being loaded by the |
Summary
Nested types |
|---|
abstract class PositionalDataSource.LoadInitialCallback<T : Any?>This class is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
open class PositionalDataSource.LoadInitialParamsThis class is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
abstract class PositionalDataSource.LoadRangeCallback<T : Any?>This class is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
open class PositionalDataSource.LoadRangeParamsThis class is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
Public companion functions |
||
|---|---|---|
Int |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
Int |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
Public constructors |
|
|---|---|
<T : Any> This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
Public functions |
||
|---|---|---|
abstract Unit |
@WorkerThreadThis function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
abstract Unit |
@WorkerThreadThis function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
final PositionalDataSource<V> |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
final PositionalDataSource<V> |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
final PositionalDataSource<V> |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
final PositionalDataSource<V> |
This function is deprecated. PositionalDataSource is deprecated and has been replaced by PagingSource |
android
|
Inherited functions |
|||||||||
|---|---|---|---|---|---|---|---|---|---|
|
Inherited properties |
|---|
Public companion functions
computeInitialLoadPosition
funcomputeInitialLoadPosition(
params: PositionalDataSource.LoadInitialParams,
totalCount: Int
): Int
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 | |
|---|---|
params: PositionalDataSource.LoadInitialParams |
Params passed to |
totalCount: Int |
Total size of the data set. |
| Returns | |
|---|---|
Int |
Position to start loading at. |
| See also | |
|---|---|
computeInitialLoadSize |
computeInitialLoadSize
funcomputeInitialLoadSize(
params: PositionalDataSource.LoadInitialParams,
initialLoadPosition: Int,
totalCount: Int
): Int
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 | |
|---|---|
params: PositionalDataSource.LoadInitialParams |
Params passed to |
initialLoadPosition: Int |
Value returned by |
totalCount: Int |
Total size of the data set. |
| Returns | |
|---|---|
Int |
Number of items to load. |
| See also | |
|---|---|
computeInitialLoadPosition |
Public constructors
PositionalDataSource
<T : Any>PositionalDataSource()
| Parameters | |
|---|---|
<T : Any> |
Type of items being loaded by the |
Public functions
loadInitial
@WorkerThread
abstract funloadInitial(
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 funloadRange(
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
final 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. |
| Returns | |
|---|---|
PositionalDataSource<V> |
A new DataSource, which transforms items using the given function. |
map
final 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. |
| Returns | |
|---|---|
PositionalDataSource<V> |
A new DataSource, which transforms items using the given function. |
mapByPage
final 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. |
| Returns | |
|---|---|
PositionalDataSource<V> |
A new DataSource, which transforms items using the given function. |
mapByPage
final 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. |
| Returns | |
|---|---|
PositionalDataSource<V> |
A new |