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.

Extension functions

final @NonNull LiveData<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> LivePagedListKt.toLiveData(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    @NonNull Executor fetchExecutor
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull LiveData<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> LivePagedListKt.toLiveData(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    @NonNull Executor fetchExecutor
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Flowable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Flowable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Flowable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Flowable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Observable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Observable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Observable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

final @NonNull Observable<@NonNull PagedList<@NonNull Value>>
<Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

This method is deprecated. PagedList is deprecated and has been replaced by PagingData

Public constructors

Factory

public <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

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

create

Added in 2.0.0
public abstract @NonNull 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
@NonNull DataSource<@NonNull Key, @NonNull Value>

the new DataSource.

map

Added in 2.0.0
public @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.

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
@NonNull DataSource.Factory<@NonNull Key, @NonNull ToValue>

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

mapByPage

Added in 2.0.0
public @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.

Same as map, but allows for batch conversions.

Parameters
<ToValue extends Object>

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

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

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

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

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

See also
map
map
mapByPage

Extension functions

LivePagedListKt.toLiveData

public final @NonNull LiveData<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> LivePagedListKt.toLiveData(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    @NonNull Executor fetchExecutor
)

Constructs a LiveData<PagedList>, from this DataSource.Factory, convenience for LivePagedListBuilder.

No work (such as loading) is done immediately, the creation of the first PagedList is deferred until the LiveData is observed.

Parameters
@NonNull PagedList.Config config

Paging configuration.

Key initialLoadKey

Initial load key passed to the first PagedList / PagingSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

@NonNull Executor fetchExecutor

Executor for fetching data from PagingSources.

LivePagedListKt.toLiveData

public final @NonNull LiveData<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> LivePagedListKt.toLiveData(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    @NonNull Executor fetchExecutor
)

Constructs a LiveData<PagedList>, from this DataSource.Factory, convenience for LivePagedListBuilder.

No work (such as loading) is done immediately, the creation of the first PagedList is deferred until the LiveData is observed.

Parameters
int pageSize

Page size.

Key initialLoadKey

Initial load key passed to the first PagedList / PagingSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

@NonNull Executor fetchExecutor

Executor for fetching data from DataSources.

RxPagedListKt.toFlowable

public final @NonNull Flowable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
@NonNull PagedList.Config config

Paging configuration.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

@NonNull BackpressureStrategy backpressureStrategy

BackpressureStrategy for the Flowable to use.

RxPagedListKt.toFlowable

public final @NonNull Flowable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
@NonNull PagedList.Config config

Paging configuration.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

@NonNull BackpressureStrategy backpressureStrategy

BackpressureStrategy for the Flowable to use.

RxPagedListKt.toFlowable

public final @NonNull Flowable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
int pageSize

Page size.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

@NonNull BackpressureStrategy backpressureStrategy

BackpressureStrategy for the Flowable to use.

RxPagedListKt.toFlowable

public final @NonNull Flowable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toFlowable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler,
    @NonNull BackpressureStrategy backpressureStrategy
)

Constructs a Flowable<PagedList>, from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Flowable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
int pageSize

Page size.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

@NonNull BackpressureStrategy backpressureStrategy

BackpressureStrategy for the Flowable to use.

RxPagedListKt.toObservable

public final @NonNull Observable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
@NonNull PagedList.Config config

Paging configuration.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

RxPagedListKt.toObservable

public final @NonNull Observable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    @NonNull PagedList.Config config,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
@NonNull PagedList.Config config

Paging configuration.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

RxPagedListKt.toObservable

public final @NonNull Observable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
int pageSize

Size of pages to load.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.

RxPagedListKt.toObservable

public final @NonNull Observable<@NonNull PagedList<@NonNull Value>> <Key extends Object, Value extends Object> RxPagedListKt.toObservable(
    @NonNull DataSource.Factory<@NonNull Key, @NonNull Value> receiver,
    int pageSize,
    Key initialLoadKey,
    PagedList.BoundaryCallback<@NonNull Value> boundaryCallback,
    Scheduler fetchScheduler,
    Scheduler notifyScheduler
)

Constructs a Observable<PagedList> from this DataSource.Factory, convenience for RxPagedListBuilder.

The returned Observable will already be subscribed on the fetchScheduler, and will perform all loading on that scheduler. It will already be observed on notifyScheduler, and will dispatch new PagedLists, as well as their updates to that scheduler.

Parameters
int pageSize

Size of pages to load.

Key initialLoadKey

Initial load key passed to the first PagedList / DataSource.

PagedList.BoundaryCallback<@NonNull Value> boundaryCallback

The boundary callback for listening to PagedList load state.

Scheduler fetchScheduler

Scheduler used to fetch from DataSources, generally a background thread pool for e.g. I/O or network loading.

Scheduler notifyScheduler

Scheduler that receives PagedList updates, and where PagedList.Callback calls are dispatched. Generally, this is the UI / main thread.