Skip to content

Relation Paging

API operations described above provide a way to retrieve a limited (initial) set of related data. In most real world scenarios, there may be more related objects than returned by Single Step, Two Step or Relation Depth Retrieval APIs. To obtain a complete set of related objects Backendless supports Relation Paging API described below.

Important

  1. To understand the concept of paged data retrieval, see the Data retrieval with Paging section of the documentation.
    2. When working with paging, it may be important to know the total size of the related collection. See the Get Object Count section for details on how to get the related objects count.

Android and Java applications must use com.backendless.persistence.LoadRelationsQueryBuilder which facilitates retrieval of related objects for a parent object. The class is responsible for maintaining the page size and offset parameters used in the paging process.

Before an instance of LoadRelationsQueryBuilder can be used in a data retrieval request, it must be initialized as shown below. Notice the setRelationName call which identifies a related column for which the related objects will be loaded:

LoadRelationsQueryBuilder<Map<String, Object>> loadRelationsQueryBuilder;
loadRelationsQueryBuilder = LoadRelationsQueryBuilder.ofMap();
loadRelationsQueryBuilder.setRelationName( "NAME-OF-RELATED-COLUMN" );
LoadRelationsQueryBuilder<CHILDCLASS> loadRelationsQueryBuilder;

// Generic reference to CHILDCLASS is needed so that related objects
// from the servers are returned to the client as instances of CHILDCLASS.
loadRelationsQueryBuilder = LoadRelationsQueryBuilder.of( CHILDCLASS.class );
loadRelationsQueryBuilder.setRelationName( "NAME-OF-RELATED-COLUMN" );

where:

Argument                Description
CHILDCLASS Used in the "Custom Class" approach only. A Java class which identifies the related table.
NAME-OF-RELATED-COLUMN Name of the related column in the parent table for which the related objects will be retrieved.

Once a query builder object is created, the following API can be used to retrieve a page of related objects:

// asynchronous API
Backendless.Data.of( "PARENT-TABLE-NAME" ).loadRelations( 
                            String parentObjectId,
                            LoadRelationsQueryBuilder queryBuilder,
                            AsyncCallback<List<Map<String, Object>>> callback );

// synchronous API
List<Map<String, Object>> relatedObjects;
relatedObjects = Backendless.Data.of( "PARENT-TABLE-NAME" ).loadRelations( 
                            String parentObjectId,
                            LoadRelationsQueryBuilder queryBuilder );
// asynchronous API
Backendless.Data.of( PARENT-CLASS ).loadRelations( 
                      LoadRelationsQueryBuilder<List<Map<String, Object>>> queryBuilder,
                      AsyncCallback<List<CHILD-CLASS>> callback );

// synchronous API
List<CHILD-CLASS> relatedObjects;
relatedObjects = Backendless.Data.of( PARENT-CLASS ).loadRelations( 
                      LoadRelationsQueryBuilder<CHILD-CLASS queryBuilder );

where:

Argument                Description
parentObjectId  Id of the object for which the related objects will be retrieved.
PARENT-TABLE-NAME Name of the table which contains the parent object identified by parentObjectId.
queryBuilder LoadRelationsQueryBuilder initialized as shown above. Used in subsequent calls to request additional pages of related data objects.
callback A responder object which will receive a callback when a page of related objects is retrieved from the server. Applies to the asynchronous method only.

The LoadRelationsQueryBuilder class contains methods which recalculate offset in order to obtain next, previous or a specific page. After loadRelationsQueryBuilder is modified, call the API shown above to get next, previous or a specific page of data.

Argument                Description
table name Name of the data table from where the objects are retrieved.