Backendless Support
 

Data retrieval API or How to load objects from an mBaaS storage

Loading data objects from the Backendless persistent storage is a fundamental operation a large majority of the online/mobile applications require. 

Backendless data retrieval API is simple, yet very powerful. 

Reviewing the Knowledge Base topics one finds out that the API provides the following capabilities:

All the features listed above are based on the core data loading API. 

Consider the example below, but make sure that the Backendless backend contains some data. 

The example in this topict (as all the other related posts highlighting the data retrieval features) will use the Restaurant To-Go App schema. It is possible to populate the backend with data using the import feature:

  1. Download the file from the URL below: https://backendless.com/documentation/samples/restaraunt-app-tables-with-data.zip
  2. Follow the instructions on how to import schema/data into a Backendless application. It will be necessary to import the file from step above.

The sample code below will be fetching data from the following table: 

Additionally, the example below will be using the code generated by Backendless.  To get the code make sure to follow the instructions describing how to generate client-side code based on the backend's database schema

                                         For JavaScript implementation CLICK HERE

JAVA

Asynchronous API (applies to 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( "Restaurant name = " + restaurant.getName() );
 }
 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 API (Applies only to 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("Restaurant name = " + restaurant.getName());
 }
 System.out.println( "Total time (ms) - " + (System.currentTimeMillis() - startTime ));
}

This example demonstrates the usage of the data loading API in its most basic form. The highlighted lines show the actual API. It returns a collection of objects. 

NOTICE: the collection is strongly-typed - there is no need to perform any additional unmarshalling of data. 

Also, the same data object which is saved on the backend is automatically morphed into the client-side type. 

In other words, the same restaurant object can arrive as a Java POJO to Android/Java application or an Objective-C or Swift object to an iOS app.

The code produces the following output:
============ Fetching first page using the SYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = Cantina Laredo
Restaurant name = Buca Di Bepo
Restaurant name = McDonald's
Restaurant name = Endless Sweets
Total time (ms) - 275
============ Fetching first page using the ASYNC API ============
Loaded 4 restaurant objects
Total restaurants in the Backendless storage - 4
Restaurant name = Cantina Laredo
Restaurant name = Buca Di Bepo
Restaurant name = McDonald's
Restaurant name = Endless Sweets
Total time (ms) - 122

The example above prints out the names of the restaurants. The code implies that the results are paged (see the getCurrentPage call).  

Review related topics:

Is article helpful?