If you played or used 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 how to architect your app in a certain way.
In this article I’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, login to your account in Backendless Console, select an app and click the Business Logic 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:
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:
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:
- Declare the resultList variable which will eventually hold all the objects that you’ll be retrieving;
- 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;
- 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;
- In order to properly insert the objects retrieved into the results list, define an offSet (set to initially).
- 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:
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 in 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:
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:
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 a 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:
Once invoked, the RESPONSE tab will display the response of your API Service:
One of the greatest features of Backendless is that you can generate a client SDK for your service. The generates SDK contains all the code you can use 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):
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.