Skip to content

Data retrieval with Paging

Backendless operations returning a collection of objects automatically break up the complete result set into pages. Backendless paging uses two parameters to enable paged data retrieval:

  • page size - identifies how many objects a single page contains
  • offset - identifies the position of the object from which to retrieve the number of records identified by page size

The diagrams below illustrate the paging process. The first diagram shows the first request to retrieve a collection of records. The request includes the parameters of 5 objects in the page, starting from offset 0:

findrequest1

To retrieve the next page of data, the client must set the offset to the index of the first not retrieved object, which is 5:

findrequest2

Important

Backendless server sets the maximum allowed value for the page size to 100 objects. The minimum value is 1.

Android applications must use the DataQueryBuilder class to set the paging parameters. The example below configures DataQueryBuilder to load 25 objects starting from offset 50. The query is used to retrieve objects from the Person table:

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setPageSize( 25 ).setOffset( 50 );
Backendless.Data.of( "Person" ).find( queryBuilder, 
                                      new AsyncCallback<List<Map>>()
  {
    @Override
    public void handleResponse( List<Map> response )
    {
      // the "response" object is a collection of java.util.Map objects.
      // each item in the collection represents an object from the "Person" table
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
  });
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setPageSize( 25 ).setOffset( 50 );
Backendless.Data.of( Person.class ).find( queryBuilder, 
                                          new AsyncCallback<List<Person>>()
  {
    @Override
    public void handleResponse( List<Person> response )
    {
      // the "response" object is a collection of Person objects.
      // each item in the collection represents an object from the "Person" table 
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
    });

Application can use the same queryBuilder object to recalculate offset (and if necessary change page size) and use it to retrieve additional pages from the server using the same data retrieval method:

// calculate offset to get the next page of data
queryBuilder.prepareNextPage();

// retrieve the next page of data using the same API as for the first page
Backendless.Data.of( "Person" ).find( queryBuilder,
                                      new AsyncCallback<List<Map>>()
  {
    @Override
    public void handleResponse( List<Map> response )
    {
      // the "response" object is a collection of java.util.Map objects.
      // each item in the collection represents an object from the "Person" table
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
  });
// calculate offset to get the next page of data
queryBuilder.prepareNextPage();

// retrieve the next page of data using the same API as for the first page
Backendless.Data.of( Person.class ).find( queryBuilder, 
                                          new AsyncCallback<List<Person>>()
  {
    @Override
    public void handleResponse( List<Person> response )
    {
      // the "response" object is a collection of Person objects.
      // each item in the collection represents an object from the "Person" table 
    }

    @Override
    public void handleFault( BackendlessFault fault )
    {
       // use the getCode(), getMessage() or getDetail() on the fault object
       // to see the details of the error
    }
    });