Skip to content

Retrieval with Relation Depth

The Data Service API supports a mechanism for loading related objects without identifying each by its name. Instead, the API includes a parameter which specifies the "depth" of the relations to include into the response. Consider the following diagram:

relation-depth

The diagram shows a hierarchy for class structure - the Order class has two relations: with OrderItem and Customer classes. Each in turn has a relation to the Manufacturer and Address classes. When an instance or a collection of Order objects is retrieved from Backendless, the API may include a parameter specifying the depth of relations to include into the response. If the relation depth is 1, then all related instances of OrderItem and Customer will be included into each Order object. If the relation depth is 2, then not only OrderItem and Customer instances will be included, but the corresponding Manufacturer and Address objects as well.

Important

Loading relations with relation depth retrieves only a partial set of the related objects (default size of the retrieved related collection is 10). To load additional related objects, use the Relation Paging API.

API methods supporting relations depth

For the methods which accept the DataQueryBuilder argument, the relation depth must be set using the SetRelationDepth method on the query builder object:SwiftSDK.framework

Blocking methods

Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ).FindById( string id, int relationsDepth );
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ).FindById( string id, 
                                                                            IList<string> relations, 
                                                                            int relationsDepth );

// Set the relation depth on the QueryBuilder instance using the SetRelationDepth method
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ).FindFirst( DataQueryBuilder queryBuilder );
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ).FindLast( DataQueryBuilder queryBuilder );
IList<Dictionary<String, Object>> Backendless.Data.Of( "TABLE-NAME" ).Find( DataQueryBuilder queryBuilder );
Task<T> Backendless.Data.Of<T>().FindByIdAsync( string id, int relationsDepth );
Task<T> Backendless.Data.Of<T>().FindByIdAsync( string id, 
                                                      IList<string> relations, 
                                                      int relationsDepth );

// Set the relation depth on the QueryBuilder instance using the SetRelationDepth method
Task<T> Backendless.Data.Of<T>().FindFirstAsync( DataQueryBuilder queryBuilder );
Task<T> Backendless.Data.Of<T>().FindLastAsync( DataQueryBuilder queryBuilder );
Task<IList<T>> Backendless.Data.Of<T>().FindAsync( DataQueryBuilder queryBuilder );

Non-blocking methods

Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ) FindById( string id, int relationsDepth );
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ) FindById( string id, 
                                                                            IList<string> relations, 
                                                                            int relationsDepth );

// Set the relation depth on the QueryBuilder instance using the SetRelationDepth method
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ).FindFirst( DataQueryBuilder queryBuilder );
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ) FindLast( DataQueryBuilder queryBuilder );
Dictionary<String, Object> Backendless.Data.Of( "TABLE-NAME" ) Find( DataQueryBuilder queryBuilder );
Prepare LoadRelationsQueryBuilder:
var loadRelationsQueryBuilder = LoadRelationsQueryBuilder<BackendlessUser>.Create();
loadRelationsQueryBuilder.SetRelationName( "friends" );
Blocking call:
String parentObjectId = ""; // removed for brevity
var friends = Backendless.Data.Of<Person>().LoadRelations( parentObjectId, 
                                                           loadRelationsQueryBuilder );
foreach( var friend in friends )
  System.Console.WriteLine( friend.Email );
Non-blocking call:
String parentObjectId = ""; // removed for brevity
var task = Backendless.Data.Of<Person>().LoadRelationsAsync( parentObjectId, 
                                                                loadRelationsQueryBuilder );
task.Wait();
var friends = task.Result;

foreach( var friend in friends )
  System.Console.WriteLine( friend.Email );

Example

var queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetRelationsDepth( 2 );
var invoices = Backendless.Data.Of( "Invoices" ).Find( queryBuilder );