Skip to content

Most visited

Recently visited



public abstract class TiledDataSource
extends DataSource<Integer, Type>

   ↳ android.arch.paging.DataSource<java.lang.Integer, Type>
     ↳ android.arch.paging.TiledDataSource<Type>

Position-based data loader for fixed size, arbitrary positioned loading.

Extend TiledDataSource if you want to load arbitrary pages based solely on position information, and can generate pages of a provided fixed size.

Room can generate a TiledDataSource for you:

 interface UserDao {
     @Query("SELECT * FROM user ORDER BY mAge DESC")
     public abstract TiledDataSource<User> loadUsersByAgeDesc();
Under the hood, Room will generate code equivalent to the below, using a limit/offset SQL query:
 interface UserDao {
     @Query("SELECT COUNT(*) from user")
     public abstract Integer getUserCount();

     @Query("SELECT * from user ORDER BY mName DESC LIMIT :limit OFFSET :offset")
     public abstract List<User> userNameLimitOffset(int limit, int offset);

 public class OffsetUserQueryDataSource extends TiledDataSource<User> {
     private MyDatabase mDb;
     private final UserDao mUserDao;
     private final InvalidationTracker.Observer mObserver;

     public OffsetUserQueryDataSource(MyDatabase db) {
         mDb = db;
         mUserDao = db.getUserDao();
         mObserver = new InvalidationTracker.Observer("user") {
             public void onInvalidated(@NonNull Set<String> tables) {
                 // the user table has been invalidated, invalidate the DataSource

     public boolean isInvalid() {
         return super.isInvalid();

     public int countItems() {
         return mUserDao.getUserCount();

     public List<User> loadRange(int startPosition, int loadCount) {
         return mUserDao.userNameLimitOffset(loadCount, startPosition);


Inherited fields

From class android.arch.paging.DataSource

Public constructors


Public methods

abstract int countItems()

Number of items that this DataSource can provide in total.

abstract List<Type> loadRange(int startPosition, int count)

Called to load items at from the specified position range.

Inherited methods

From class android.arch.paging.DataSource
From class java.lang.Object

Public constructors


TiledDataSource ()

Public methods


int countItems ()

Number of items that this DataSource can provide in total.

int Number of items this DataSource can provide. Must be 0 or greater.


List<Type> loadRange (int startPosition, 
                int count)

Called to load items at from the specified position range.

This method must return a list of requested size, unless at the end of list. Fixed size pages enable TiledDataSource to navigate tiles efficiently, and quickly accesss any position in the data set.

If a list of a different size is returned, but it is not the last list in the data set based on the return value from countItems(), an exception will be thrown.

startPosition int: Index of first item to load.

count int: Number of items to load.

List<Type> List of loaded items, of the requested length unless at end of list. Null if the DataSource is no longer valid, and should not be queried again.

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


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)