Skip to content

Most visited

Recently visited

navigation

TiledDataSource

public abstract class TiledDataSource
extends DataSource<Integer, Type>

java.lang.Object
   ↳ 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:

 @Dao
 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:
 @Dao
 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;
     @SuppressWarnings("FieldCanBeLocal")
     private final InvalidationTracker.Observer mObserver;

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

     @Override
     public boolean isInvalid() {
         mDb.getInvalidationTracker().refreshVersionsSync();
         return super.isInvalid();
     }

     @Override
     public int countItems() {
         return mUserDao.getUserCount();
     }

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

Summary

Inherited fields

From class android.arch.paging.DataSource

Public constructors

TiledDataSource()

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

TiledDataSource ()

Public methods

countItems

int countItems ()

Number of items that this DataSource can provide in total.

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

loadRange

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.

Parameters
startPosition int: Index of first item to load.

count int: Number of items to load.

Returns
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

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)