Message:

Subscribe rss
Blog categories
All | Backendless features
Feature 22: Loading related data objects – the ‘one-step/dynamic’ approach
January 29, 2015 by markpiller
Spread the love

In my previous post I described how to load complex data objects from the persistent storage using the “auto-load” technique. Using that approach a developer can statically identify specific (child) properties which should be returned along with the parent object(s) when a client app sends a request to load them. That approach works well for object relations which must be unconditionally returned with the parent object. However, there are many scenarios when the client app must control which relations should be preloaded and returned along with the parent.  The API described below allows to accomplish that.

Consider the following example which uses the client-side code generated by Backendless based on the database schema. In that schema the Restaurant table has the “locations” column which represents a one-to-many relationship between the Restaurant and Locations table. This means that a restaurant may have several related locations as shown in the screenshots below:

The restaurant objects:
restaurant objects - Feature 22: Loading related data objects - the 'one-step/dynamic' approach

The related location object for the “Cantina Laredo” restaurant:
related locations - Feature 22: Loading related data objects - the 'one-step/dynamic' approach

Synchronous API (Plain Java only):

Asynchronous API (Android and Plain Java):

Synchronous API:

Asynchronous API:

Synchronous API:

Asynchronous API:

Asynchronous sample:

Synchronous sample:

Notice the addRelated method in the QueryOptions class in the Java, Objective-C and Swift examples. The method requests that the data objects referenced by “locations” column must be initialized and returned with the every parent Restaurant object. The JavaScript example uses the relations array in the QueryOptions object for the same purpose. The printLocations method displays the loaded Locations objects:

The code above produces the following output. Notice that the “Cantina Laredo” restaurant is fetched with the related location:

See the documentation for more information on loading related objects with the “one-step” approach.

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+