Skip to content

Most visited

Recently visited

navigation

DataSource

public abstract class DataSource
extends Object

java.lang.Object
   ↳ android.arch.paging.DataSource<Key, Value>
Known Direct Subclasses


Base class for incremental data loading, used in list paging. To implement, extend either the KeyedDataSource, or TiledDataSource subclass.

Choose based on whether each load operation is based on the position of the data in the list.

Use KeyedDataSource if you need to use data from item N-1 to load item N. For example, if requesting the backend for the next comments in the list requires the ID or timestamp of the most recent loaded comment, or if querying the next users from a name-sorted database query requires the name and unique ID of the previous.

Use TiledDataSource if you can load arbitrary pages based solely on position information, and can provide a fixed item count. TiledDataSource supports querying pages at arbitrary positions, so can provide data to PagedLists in arbitrary order.

Because a null item indicates a placeholder in PagedList, DataSource may not return null items in lists that it loads. This is so that users of the PagedList can differentiate unloaded placeholder items from content that has been paged in.

Summary

Nested classes

interface DataSource.InvalidatedCallback

Invalidation callback for DataSource. 

Fields

public static int COUNT_UNDEFINED

If returned by countItems(), indicates an undefined number of items are provided by the data source.

Public methods

void addInvalidatedCallback(DataSource.InvalidatedCallback onInvalidatedCallback)

Add a callback to invoke when the DataSource is first invalidated.

abstract int countItems()

Number of items that this DataSource can provide in total, or COUNT_UNDEFINED.

void invalidate()

Signal the data source to stop loading, and notify its callback.

boolean isInvalid()

Returns true if the data source is invalid, and can no longer be queried for data.

void removeInvalidatedCallback(DataSource.InvalidatedCallback onInvalidatedCallback)

Remove a previously added invalidate callback.

Inherited methods

From class java.lang.Object

Fields

COUNT_UNDEFINED

int COUNT_UNDEFINED

If returned by countItems(), indicates an undefined number of items are provided by the data source. Continued querying in either direction may continue to produce more data.

Public methods

addInvalidatedCallback

void addInvalidatedCallback (DataSource.InvalidatedCallback onInvalidatedCallback)

Add a callback to invoke when the DataSource is first invalidated.

Once invalidated, a data source will not become valid again.

A data source will only invoke its callbacks once - the first time invalidate() is called, on that thread.

Parameters
onInvalidatedCallback DataSource.InvalidatedCallback: The callback, will be invoked on thread that invalidate() is called on.

countItems

int countItems ()

Number of items that this DataSource can provide in total, or COUNT_UNDEFINED.

Returns
int number of items that this DataSource can provide in total, or COUNT_UNDEFINED if expensive or undesired to compute.

invalidate

void invalidate ()

Signal the data source to stop loading, and notify its callback.

If invalidate has already been called, this method does nothing.

isInvalid

boolean isInvalid ()

Returns true if the data source is invalid, and can no longer be queried for data.

Returns
boolean True if the data source is invalid, and can no longer return data.

removeInvalidatedCallback

void removeInvalidatedCallback (DataSource.InvalidatedCallback onInvalidatedCallback)

Remove a previously added invalidate callback.

Parameters
onInvalidatedCallback DataSource.InvalidatedCallback: The previously added callback.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)