Blog

Using Codeless to Retrieve More Than 100 Objects With a Single Client-Side API Call

by on October 16, 2018

If you’ve used the Data Retrieval API in Backendless Cloud, you may know that the server limits the number of objects retrieved from a table to 100 in a single call. For Managed Backendless and for Backendless Pro, this limit is configurable.  In order to retrieve more than 100 objects, data paging is required. Paging greatly improves your application performance but requires you to think about how to architect your app in a certain way.

In this article, we’ll describe how to get more than 100 objects, while using the minimum number of API calls, and do it without writing any code at all.  Using this methodology, all that is needed to retrieve all objects from the database is a single call from the client application to the Backendless server.

The solution you will develop will be implemented as a custom API service. You will use the Codeless programming to implement the logic of the service.

To get started, log in to your account in Backendless Console, select an app and click the Business Logic (Cloud Code) icon. The API SERVICES tab is selected by default. Click the plus icon to create a new API Service. You will see the New Service popup. Select the CODELESS tab in the popup and enter a name for your service:

20181004 3
Click SAVE to create the service. Once the service is created, you will be prompted to add a new method for your service (see the popup below). Enter a name for your method in the Method Name field, I called it findObjectsWithPaging. In order to make the method generic, so that it can work with any data table, add two method parameters as shown below – tableName and whereClause:
20181004 4
Click SAVE. Backendless finalizes the creation of the service’s method. Locate the service in the list of API Services (if this is your first service, there will be only one in the list), click the service name – the first (and only) method will be selected and you will see the method’s logic preview. Click the EDIT button in the LOGIC tab to open the Codeless Logic Designer.

The first step in creating the logic for the method is declaring and initializing variables:

  1. Declare the resultList variable which will eventually hold all the objects that you’ll be retrieving;
  2. Determine how many objects you will be retrieving by declaring and initializing the numberofObjects variable. The variable will be initialized with the value returned from the Get Object Count block which uses the provided table name TableName and the where clause whereClause parameters;
  3. Declare the pagesQuantity variable which will contain the number of 100 object pages the final result set consists of. The value is calculated as numberOfObjects divided by 100 (where 100 is pageSize), and rounded up to the nearest integer;
  4. In order to properly insert the objects retrieved into the results list, define an offSet (set to 0 initially).
  5. The sortBy variable is completely optional. When initialized as shown below (with the “position” string), it assumes your table has the “position” column and the final result set will be ordered by the values from that column.

For example, if numberOfObjects is 533, pagesQuantity will be 6 (533/100 = 5.33, rounded up to 6).

The Codeless Business Logic will look like this:
20181004 5
The next step is creating a loop which will have pagesQuantity iterations. Each iteration of the loop will get up to 100 objects from TableName using the provided whereClause condition. The retrieved objects will be inserted into the resultList variable. Note that in every iteration of the loop, the offSet variable is incremented by 100. Also, the sortBy variable is used to sort the resulting collection (this is entirely optional and assumes you have the position column). The logic of the loop should look as shown below: 
20181004 6
Finally, you need to combine the blocks with variable declarations and the loop and then return resultList from your method. The final logic will look as shown below:
20181004 7
Now that the logic is complete, click the DEPLOY MODEL button to deploy your Codeless service into Backendless. Once deployed, click the API SERVICES tab to return to the API Services listing.

Select the PARAMETERS tab and enter the name of a table along with a where clause and invoke the service by clicking the INVOKE button. Notice that the arguments must be formatted as a JSON object:

{
  "TableName":"YOUR-TABLENAME",
  "whereClause":"YOUR-WHERE-CLAUSE"
}

Once invoked, the RESPONSE tab will display the response of your API Service:
20181004 8
One of the greatest features of Backendless is that you can generate a client SDK for your service. The generated SDK contains all the code you need in a client app to invoke your service (which now can be done with a single API call to get all the objects from the database):
20181004 9
With this API service in place, all the routine work for retrieving bulk data is automated, making the user side of the application much simpler and faster. 

If you have any questions about this procedure, please post your questions in our support forum (https://support.backendless.com) or on Slack (http://slack.backendless.com).

1 Comment

This is a great tutorial. Widely useful in applications and demonstrates some great non-obvious concepts in Backendless.

Leave a Reply