{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
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. |
Public methods |
|
---|---|
final @NonNull Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> |
asPagingSourceFactory(@NonNull CoroutineDispatcher fetchDispatcher) |
abstract @NonNull DataSource<@NonNull Key, @NonNull Value> |
create() Create a |
@NonNull DataSource.Factory<@NonNull Key, @NonNull ToValue> |
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( Applies the given function to each value emitted by DataSources produced by this Factory. |
@NonNull
public final Function0<@NonNull PagingSource<@NonNull Key, @NonNull Value>> asPagingSourceFactory(@NonNull CoroutineDispatcher fetchDispatcher)
@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. |
@NonNull
public DataSource.Factory<@NonNull Key, @NonNull ToValue> <ToValue extends Object> map(
@NonNull <ERROR CLASS><@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 |
@NonNull <ERROR CLASS><@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 |
@NonNull
public DataSource.Factory<@NonNull Key, @NonNull ToValue> <ToValue extends Object> mapByPage(
@NonNull <ERROR CLASS><@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 map
, but allows for batch conversions.
Parameters | |
---|---|
<ToValue extends Object> |
Type of items produced by the new |
@NonNull <ERROR CLASS><@NonNull List<@NonNull Value>, @NonNull List<@NonNull ToValue>> function |
Function that runs on each loaded page, returning items of a potentially new type. |
Returns | |
---|---|
DataSource.Factory<@NonNull Key, @NonNull ToValue> |
A new |