Message:

Subscribe rss
Blog categories
All | Backendless features
Feature 28: Loading related data objects. The semi-lazy approach
February 4, 2015 by markpiller
Spread the love

Previously I wrote an about how to load object relations using the auto-load and the one-step approaches. 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 substandard user experience. Additionally, an entity stored in the Backendless backend 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 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:

  1. Download the ZIP file which contains the definition for all tables from: https://backendless.com/documentation/samples/restaurant-app-schema.zip
  2. Login to console, select an app (it is recommended to create a new app for this) and click the Manage icon.
  3. Select the Import menu item.
  4. Click the single Zip file link located in the description text and browse to the ZIP file from step 1 above.

The restaurant table the sample works with looks as shown below:
restaurant table - Feature 28: Loading related data objects. The semi-lazy approach

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:

Asynchronous sample (Plain Java and Android):

Synchronous sample (Plain Java):

The printLocations method used to print out the details of the related locations is:

The output produced by these methods:

Asynchronous API sample:

Synchronous API sample:

See the documentation for more information about loading relations with the two-step approach.

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+