{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

DataSource.Factory

{% setvar page_path %}androidx/paging/DataSource.Factory.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_kotlin_switcher2.md" %}

abstract class DataSource.Factory<Key : Any, Value : Any>


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 : Any>

Key identifying items in DataSource.

<Value : Any>

Type of items in the list loaded by the DataSources.

Summary

Public constructors

<Key : Any, Value : Any> Factory()

Public functions

() -> PagingSource<Key, Value>
abstract DataSource<Key, Value>

Create a DataSource.

open DataSource.Factory<Key, ToValue>
<ToValue : Any> map(function: <ERROR CLASS><Value, ToValue>)

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

open DataSource.Factory<Key, ToValue>
<ToValue : Any> map(function: (Value) -> ToValue)

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

open DataSource.Factory<Key, ToValue>
<ToValue : Any> mapByPage(
    function: <ERROR CLASS><List<Value>, List<ToValue>>
)

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

open DataSource.Factory<Key, ToValue>
<ToValue : Any> mapByPage(function: (List<Value>) -> List<ToValue>)

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

Public constructors

Factory

<Key : Any, Value : Any> Factory()
Parameters
<Key : Any>

Key identifying items in DataSource.

<Value : Any>

Type of items in the list loaded by the DataSources.

Public functions

asPagingSourceFactory

fun asPagingSourceFactory(
    fetchDispatcher: CoroutineDispatcher = Dispatchers.IO
): () -> PagingSource<Key, Value>

create

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.

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<Key, Value>

the new DataSource.

map

open fun <ToValue : Any> map(function: <ERROR CLASS><Value, ToValue>): DataSource.Factory<Key, ToValue>

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

Same as mapByPage, but operates on individual items.

Parameters
<ToValue : Any>

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

function: <ERROR CLASS><Value, ToValue>

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

Returns
DataSource.Factory<Key, ToValue>

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

See also
mapByPage
map
mapByPage

map

open fun <ToValue : Any> map(function: (Value) -> ToValue): DataSource.Factory<Key, ToValue>

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

An overload of map that accepts a kotlin function type.

Same as mapByPage, but operates on individual items.

Parameters
<ToValue : Any>

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

function: (Value) -> ToValue

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

Returns
DataSource.Factory<Key, ToValue>

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

See also
mapByPage
map
mapByPage

mapByPage

open fun <ToValue : Any> mapByPage(
    function: <ERROR CLASS><List<Value>, List<ToValue>>
): DataSource.Factory<Key, ToValue>

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

Same as map, but allows for batch conversions.

Parameters
<ToValue : Any>

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

function: <ERROR CLASS><List<Value>, List<ToValue>>

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

Returns
DataSource.Factory<Key, ToValue>

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

See also
map
map
mapByPage

mapByPage

open fun <ToValue : Any> mapByPage(function: (List<Value>) -> List<ToValue>): DataSource.Factory<Key, ToValue>

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

An overload of mapByPage that accepts a kotlin function type.

Same as map, but allows for batch conversions.

Parameters
<ToValue : Any>

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

function: (List<Value>) -> List<ToValue>

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

Returns
DataSource.Factory<Key, ToValue>

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

See also
map
map
mapByPage