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:
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:
Important
Backendless server sets the maximum allowed value for the page size to 100 objects. The minimum value is 1.
.NET 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 );
// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result = Backendless.Data.Of( "Contact" ).Find( queryBuilder );
// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
foundObjects =>
{
System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of( "Contact" ).Find( queryBuilder, findCallback );
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetPageSize( 25 ).SetOffset( 50 );
// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Contact> result = Backendless.Data.Of<Contact>().Find( queryBuilder );
// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Contact>> findCallback;
findCallback = new AsyncCallback<IList<Contact>>(
foundObjects =>
{
System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of<Contact>().Find( queryBuilder, findCallback );
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, then re-run the query
queryBuilder.PrepareNextPage();
// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result = Backendless.Data.Of( "Contact" ).Find( queryBuilder );
// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
foundObjects =>
{
System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of( "Contact" ).Find( queryBuilder, findCallback );
// calculate offset to get the next page of data, then re-run the query
queryBuilder.PrepareNextPage();
// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Contact> result = Backendless.Data.Of<Contact>().Find( queryBuilder );
// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Contact>> findCallback;
findCallback = new AsyncCallback<IList<Contact>>(
foundObjects =>
{
System.Console.WriteLine( "Server returned " + foundObjects.Count + " objects" );
},
error =>
{
System.Console.WriteLine( "Server returned an error " + error.Message );
} );
Backendless.Data.Of<Contact>().Find( queryBuilder, findCallback );