Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

Easily Make Asynchronous REST Calls In Delphi 10.2.1 Tokyo On Android, iOS, macOS, and Windows 10

The Hospitality Survey App template contains a RAD Server project and a Delphi client project. You can download the Hospitality Survey App template code for yourself from Embarcadero’s GetIt. The Delphi client project makes REST calls via the BaaS components to RAD Server. Keep in mind that this same source code will work with TRESTClient and TNetHTTPClient. It is beneficial for the REST calls to be made in an asynchronous way because the interface stays responsive and on some platforms like Android it is required that HTTP calls are made asynchronously.

There are quite a few different ways to achieve asynchronous calls using the various libraries and functionality built into Delphi 10.2.1 Tokyo. The below source code is one way to make asynchronous calls that I found which worked for me. It uses the Parallel Programming Library with it’s TTask object in this case. If you’re interested in more on how TTask works check out this tutorial as well. It includes examples of pausing and canceling tasks.

In the below code a TTask is used to create a thread where the BackendEndpointGet component calls it’s Execute method. The BackendEndpointGet.Tag property is used to store whether the BackendEndpointGet component is in use or not (BUSY or NOT_BUSY) so that two calls can not be made on the same component at the same time. The activity dialog is show prior to starting the Task. The Execute method is synchronous but it is inside of the Task so it doesn’t block the app UI and the activity dialog displays nicely over it. Once the Execute method completes the code checks that the response code from the call is the HTTP 200 code. If it is then is proceeds to save out the contents of the response to a JSON file. TThread.Synchronize() is then used to make sure that any changes to the UI take place within the main thread. In this case the JSON file is loaded into a TFDMemTable and then the dynamic survey form is built using the GenerateSurvey() procedure from the contents of the TFDMemTable.

After all of the data is loaded or an exception is encountered, the activity dialog is hidden again and the hidden activity dialog procedure is within a TThread.Synchronize call as well so that the changes to the UI are main from within the main thread.

As I previously started there are a lot of different ways to do this but this is one way I found that worked for me. Check out the source code below!

Want more info about how the Hospitality Survey App template works? Check out the full Developers Guide in this video.

 

Do you want to test and make Asynchronous REST Calls for iOS, Android, macOS, and Windows? Try the Cross-Platform Development Frameworks, which can help you create apps that support database servers in the Delphi or C++ environments.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

FMXExpress.com has over 600 articles with all kinds of tips and tricks for Delphi FireMonkey on Android, IOS, OSX, Windows, and Linux.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES