To help with this, the provided query object will automatically contain a reference to the item immediately before the start of the first new item to fetch if available.
The item immediately after the end of the range to fetch might also be available in some cases if the user is scrolling through the data backwards. There are, however, no guarantees that either item will be available in all queries, so the implementation should always also support fetching by offset.
-
-Some backends may also use a "cursor" object that encapsulates exactly where the next page of data would continue if continuing from where the previous query ended.
-The data provider implementation can pass such instances to the [interfacename]#FetchResult# object so that the framework can include the appropriate cursor in a query that continues from where the previous query ended.
-
-As an example, a backend with such functionality could look like this:
-
-[source, java]
-----
-public interface PersonService {
- PersonFetchResult fetchPersons(
- int pageIndex,
- int pageSize);
-
- PersonFetchResult fetchPersons(
- PersonFetchCursor cursor,
- int pageSize);
-
- int getPersonCount();
-}
-
-public interface PersonFetchResult {
- List<Person> getPersons();
- PersonFetchCursor getCursor();
-}
-----
-
-A data source utilizing the cursor could look like this:
-[source, java]
-----
-public class PersonDataSource
- extends BackendDataSource<Person> {
-
- @Override
- public void fetch(Query<Person> query,
- FetchResult<Person> result) {
- PersonFetchResult personResult;
-
- Optional<?> maybeCursor = query.getNextCursor();
- if (maybeCursor.isPresent()) {
- PersonFetchCursor cursor =
- (PersonFetchCursor) maybeCursor.get();
- personResult = getPersonService().fetchPersons(
- cursor, query.getLimit());
- } else {
- personResult = getPersonService().fetchPersons(
- query.getOffset(), query.getLimit());
- }
-
- result.setNextCursor(personResult.getCursor());
- result.setItems(personResult.getPersons());
- }
-
- @Override
- public int getCount(Query<Person> query) {
- return getPersonService().getPersonCount();
- }
-}
-----
-
-The framework will automatically take care of the cursor instance stored in its [interfacename]#FetchResult# and make it available through the next query if it continues from the end offset of the query for which the cursor was stored.
-
-[NOTE]
-This simple example only uses a cursor for continuing from a previous result if going forward. A real service would also support cursors for continuing backwards. There are corresponding methods for defining a cursor in that direction; [interfacename]#FetchResult#.[methodname]#setPreviousCursor# and [interfacename]#Query#.[methodname]#getPreviousCoursor#.