Backendless Support
 

Loading related data objects - the 'auto-load' approach

While descriing the topic of retrieving data objects from Backendless, it is obviously that the code in the article loads a collection of the Restaurant objects and although it does not show it, the related collection of the Location objects arrives un-initialized. That is the default behavior of Backendless when it comes to loading related objects.

The code below demonstrates that the collection is indeed empty (null). (The code in these examples is from the article describing how to generate client-side code based on data tables).


JAVA

Method to print out related locations:

private static void printLocations( List<Location> locations )
{
 if( locations == null )
 {
 System.out.println( "Restaurant locations have not been loaded" );
 }
 else if( locations.size() == 0 )
 {
 System.out.println( "There are no related locations" );
 }
 else
 {
 Iterator<Location> iterator = locations.iterator();
 while( iterator.hasNext() )
 {
 Location location = iterator.next();
 System.out.println( "Location: Street address - " + location.getStreetAddress() + ", City - " + location.getCity() );
 }
 }
}
Asynchronous example (Android and plain Java):
private static void fetchingFirstPageAsync() throws InterruptedException
{
 long startTime = System.currentTimeMillis();
 final CountDownLatch latch = new CountDownLatch( 1 );
 AsyncCallback<BackendlessCollection<Restaurant>> callback=new AsyncCallback<BackendlessCollection<Restaurant>>()
 {
 @Override
 public void handleResponse( BackendlessCollection<Restaurant> restaurants )
 {
 System.out.println( "Loaded " + restaurants.getCurrentPage().size() + " restaurant objects" );
 System.out.println( "Total restaurants in the Backendless storage - " + restaurants.getTotalObjects() );
 Iterator<Restaurant> iterator=restaurants.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 Restaurant restaurant=iterator.next();
 System.out.println( "\nRestaurant name = " + restaurant.getName() );
 printLocations( restaurant.getLocations() );
 }
 latch.countDown();
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 }
 };
 Backendless.Data.of( Restaurant.class ).find( callback );
 latch.await();
 System.out.println( "Total time (ms) - " + (System.currentTimeMillis() - startTime ));
}
Synchronous example (plain Java):
private static void fetchingFirstPage()
{
 long startTime = System.currentTimeMillis();
 BackendlessCollection<Restaurant> restaurants = Backendless.Data.of(Restaurant.class).find();
 System.out.println( "Loaded " + restaurants.getCurrentPage().size() + " restaurant objects" );
 System.out.println( "Total restaurants in the Backendless storage - " + restaurants.getTotalObjects() );
 Iterator<Restaurant> iterator = restaurants.getCurrentPage().iterator();
 while (iterator.hasNext())
 {
 Restaurant restaurant = iterator.next();
 System.out.println( "\nRestaurant name = " + restaurant.getName() );
 printLocations( restaurant.getLocations() );
 }
 System.out.println( "Total time (ms) - " + (System.currentTimeMillis() - startTime ));
}
The code output:
============ Fetching first page using the SYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = McDonald's
Restaurant locations have not been loaded
Restaurant name = Buca Di Bepo
Restaurant locations have not been loaded
Restaurant name = Cantina Laredo
Restaurant locations have not been loaded
Restaurant name = Endless Sweets
Restaurant locations have not been loaded
Total time (ms) - 437
============ Fetching first page using the ASYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = McDonald's
Restaurant locations have not been loaded
Restaurant name = Buca Di Bepo
Restaurant locations have not been loaded
Restaurant name = Cantina Laredo
Restaurant locations have not been loaded
Restaurant name = Endless Sweets
Restaurant locations have not been loaded
Total time (ms) - 202

There are several approaches for loading related objects for a parent entity. 

This article reviews one of them - the 'auto-load' option. This option is available in Backendless console. The screenshot below shows the Restaurant table. Notice the 'auto load' checkbox in the "owner" and "locations" columns: 

When the checkbox is selected, Backendless automatically includes the related objects for the column into the response. If a developer selects the auto-load checkbox for the "locations" column and re-run the code above, you will get the following output: 

============ Fetching first page using the SYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = McDonald's
There are no related locations
Restaurant name = Buca Di Bepo
There are no related locations
Restaurant name = Cantina Laredo
Location: Street address - 123 Main St., City - Frisco
Restaurant name = Endless Sweets
There are no related locations
Total time (ms) - 548
============ Fetching first page using the ASYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = McDonald's
There are no related locations
Restaurant name = Buca Di Bepo
There are no related locations
Restaurant name = Cantina Laredo
Location: Street address - 123 Main St., City - Frisco
Restaurant name = Endless Sweets
There are no related locations
Total time (ms) - 305

As it is demonstrated in the output, the related location for the "Cantina Laredo" restaurant has been loaded without making any changes to the code. All it took is the selected checkbox in the console.

Review related topics:

Is article helpful?