DataSource.Factory

public static abstract class DataSource.Factory
extends Object

java.lang.Object
   ↳ androidx.paging.DataSource.Factory<Key, Value>


Factory for DataSources.

Data-loading systems of an application or library can implement this interface to allow LiveData<PagedList>s to be created. For example, Room can provide a DataSource.Factory for a given SQL query:

 @Dao
 interface UserDao {
    @Query("SELECT * FROM user ORDER BY lastName ASC")
    public abstract DataSource.Factory<Integer, User> usersByLastName();
 }
 
In the above sample, Integer is used because it is the Key type of PositionalDataSource. Currently, Room uses the LIMIT/OFFSET SQL keywords to page a large query with a PositionalDataSource.

Summary

Public constructors

DataSource.Factory()

Public methods

abstract DataSource<Key, Value> create()

Create a DataSource.

<ToValue> Factory<Key, ToValue> map(Function<Value, ToValue> function)

Applies the given function to each value emitted by DataSources produced by this Factory.

<ToValue> Factory<Key, ToValue> mapByPage(Function<List<Value>, List<ToValue>> function)

Applies the given function to each value emitted by DataSources produced by this Factory.

Inherited methods

Public constructors

DataSource.Factory

public DataSource.Factory ()

Public methods

create

public abstract DataSource<Key, Value> create ()

Create a DataSource.

The DataSource should invalidate itself if the snapshot is no longer valid. If a DataSource becomes invalid, the only way to query more data is to create a new DataSource from the Factory.

LivePagedListBuilder for example will construct a new PagedList and DataSource when the current DataSource is invalidated, and pass the new PagedList through the LiveData<PagedList> to observers.

Returns
DataSource<Key, Value> the new DataSource.

map

public Factory<Key, ToValue> map (Function<Value, ToValue> function)

Applies the given function to each value emitted by DataSources produced by this Factory.

Same as mapByPage(Function), but operates on individual items.

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

Returns
Factory<Key, ToValue> A new DataSource.Factory, which transforms items using the given function.

mapByPage

public Factory<Key, ToValue> mapByPage (Function<List<Value>, List<ToValue>> function)

Applies the given function to each value emitted by DataSources produced by this Factory.

Same as map(Function), but allows for batch conversions.

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

Returns
Factory<Key, ToValue> A new DataSource.Factory, which transforms items using the given function.