Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

Factory

abstract class Factory<Key : Any!, Value : Any!>
kotlin.Any
   ↳ androidx.paging.DataSource.Factory

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

Factory for DataSources.

Public methods

abstract DataSource<Key, Value>

Create a DataSource.

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

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

open DataSource.Factory<Key, ToValue>
mapByPage(@NonNull function: Function<MutableList<Value>!, MutableList<ToValue>!>)

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

Extension functions

From androidx.paging
Flowable<PagedList<Value>>
DataSource.Factory<Key, Value>.toFlowable(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null, backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST)

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

Flowable<PagedList<Value>>
DataSource.Factory<Key, Value>.toFlowable(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null, backpressureStrategy: BackpressureStrategy = BackpressureStrategy.LATEST)

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

LiveData<PagedList<Value>>
DataSource.Factory<Key, Value>.toLiveData(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor())

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

LiveData<PagedList<Value>>
DataSource.Factory<Key, Value>.toLiveData(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchExecutor: Executor = ArchTaskExecutor.getIOThreadExecutor())

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

Observable<PagedList<Value>>
DataSource.Factory<Key, Value>.toObservable(config: PagedList.Config, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null)

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

Observable<PagedList<Value>>
DataSource.Factory<Key, Value>.toObservable(pageSize: Int, initialLoadKey: Key? = null, boundaryCallback: PagedList.BoundaryCallback<Value>? = null, fetchScheduler: Scheduler? = null, notifyScheduler: Scheduler? = null)

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

Public constructors

<init>

Factory()

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.

Public methods

create

@NonNull abstract fun create(): DataSource<Key, Value>

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.

Return
DataSource<Key, Value>: the new DataSource.

map

@NonNull open fun <ToValue : Any!> map(@NonNull function: Function<Value, ToValue>): DataSource.Factory<Key, ToValue>

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<Value, ToValue>: Function that runs on each loaded item, returning items of a potentially new type.
<ToValue> Function<Value, ToValue>: Type of items produced by the new DataSource, from the passed function.
Return
DataSource.Factory<Key, ToValue>: A new DataSource.Factory, which transforms items using the given function.

mapByPage

@NonNull open fun <ToValue : Any!> mapByPage(@NonNull function: Function<MutableList<Value>!, MutableList<ToValue>!>): DataSource.Factory<Key, ToValue>

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<MutableList<Value>!, MutableList<ToValue>!>: Function that runs on each loaded page, returning items of a potentially new type.
<ToValue> Function<MutableList<Value>!, MutableList<ToValue>!>: Type of items produced by the new DataSource, from the passed function.
Return
DataSource.Factory<Key, ToValue>: A new DataSource.Factory, which transforms items using the given function.