DataSource.Factory

public abstract class DataSource.Factory<Key extends Object, Value extends Object>


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.

Parameters
<Key extends Object>

Key identifying items in DataSource.

<Value extends Object>

Type of items in the list loaded by the DataSources.

Summary

Public constructors

<Key extends Object, Value extends Object> Factory()

Public methods

final @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>>
abstract @NonNull DataSource<@NonNull Key, @NonNull Value>

Create a DataSource.

@NonNull DataSource.Factory<@NonNull Key, @NonNull ToValue>
<ToValue extends Object> map(
    @NonNull Function<@NonNull Value, @NonNull ToValue> function
)

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

@NonNull DataSource.Factory<@NonNull Key, @NonNull ToValue>
<ToValue extends Object> mapByPage(
    @NonNull Function<@NonNull List<@NonNull Value>, @NonNull List<@NonNull ToValue>> function
)

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

Public constructors

Factory

public final <Key extends Object, Value extends Object> Factory()
Parameters
<Key extends Object>

Key identifying items in DataSource.

<Value extends Object>

Type of items in the list loaded by the DataSources.

Public methods

asPagingSourceFactory

@NonNull
public final Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> asPagingSourceFactory(@NonNull CoroutineDispatcher fetchDispatcher)

create

@NonNull
public abstract DataSource<@NonNull Key, @NonNull 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.

androidx.paging.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<@NonNull Key, @NonNull Value>

the new DataSource.

map

@NonNull
public DataSource.Factory<@NonNull Key, @NonNull ToValue> <ToValue extends Object> map(
    @NonNull Function<@NonNull Value, @NonNull ToValue> function
)

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

Same as mapByPage, but operates on individual items.

Parameters
<ToValue extends Object>

Type of items produced by the new DataSource, from the passed function.

@NonNull Function<@NonNull Value, @NonNull ToValue> function

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

Returns
DataSource.Factory<@NonNull Key, @NonNull ToValue>

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

mapByPage

@NonNull
public DataSource.Factory<@NonNull Key, @NonNull ToValue> <ToValue extends Object> mapByPage(
    @NonNull Function<@NonNull List<@NonNull Value>, @NonNull List<@NonNull ToValue>> function
)

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

Same as