In other articles, we’ve discussed how to load object relations using the auto-load and the one-step approach. Both of these approaches return a complex hierarchy of data where the parent object includes child entities at the time when it is retrieved from the server.
Quite often these approaches are less than desirable for the reason that they may result in large payloads of data which may slow down data transfer and result in a suboptimal user experience. Additionally, an entity stored in the Backendless Database may have a lot of related properties and it may be desirable to fetch a specific relation after the parent object has been loaded by the client.
To enable loading of relations after the client has retrieved the parent object, Backendless provides API to load related objects with a two-step approach. The “two-step” term describes the fact that the parent entity is loaded with the first call and the second call/step is to fetch specific relations. The sample code below demonstrates retrieval of the “locations” property for the Restaurant objects. This example is based on the Restaurant-to-Go application schema.
Please follow the steps below to configure your Backendless backend with the schema and data for the application:
The restaurant table the sample works with looks as shown below:
Notice the “Cantina Laredo” restaurant has related objects in the “locations” property. The code below retrieves a collection of the Restaurant objects (first step). Then the code makes a request to load the specific relation for each restaurant object:
The printLocations method used to print out the details of the related locations is:
The printLocations method used to print out the details of the related locations is:
The printLocations method used to print out the details of the related locations is:
The printLocations method used to print out the details of the related locations is:
The printLocations method used to print out the details of the related locations is:
See the documentation for more information about loading relations with the two-step approach.
Enjoy!