Blog
All Blog Posts | Next Post | Previous PostDiving deeper: Cloudbase REST in Delphi, Part 3/3: Sync vs Async
Thursday, October 6, 2022
Intro
Today, we are continuing our cloud base REST blog series with part 3: Sync vs Async. If you missed our first blog, follow this link: https://www.tmssoftware.com/site/blog.asp?post=993. The second blog covers a couple of special cases and can be found here: https://www.tmssoftware.com/site/blog.asp?post=996. TMS FNC Core is a universal core layer for creating rich visual and non-visual components for VCL, FMX, LCL and WEB core apps. A major part of TMS FNC Core is the ability to create and execute REST requests. In this blog post we will take a look at the difference between sync and async when handling REST requests and responses.
Async
By default, each request created and executed is being processed asynchronously. This means that whenever you are calling
c.ExecuteRequest( procedure(const ARequestResult: TTMSFNCCloudBaseRequestResult) begin end );
The code will not block the caller and proceed with executing code. The request is being prepared and executed in a different thread. We can test this by adding some log statements to our original GET request.
c.Request.Clear; c.Request.Host := 'https://httpbin.org'; c.Request.Method := rmGET; c.Request.Path := '/get'; Memo1.Lines.Add('Before executing request'); c.ExecuteRequest( procedure(const ARequestResult: TTMSFNCCloudBaseRequestResult) begin Memo1.Lines.Add('Request finished'); end ); Memo1.Lines.Add('Next Code');
Before executing request Next Code Request finished
Sync
Downloading significant amounts of data such as files or larger strings, or even when working with a slower service, async benefits from allowing multiple requests being executed. In some cases however, to benefit code flow, it's better to use a synchronous call. For smaller requests such as geocoding or reverse geocoding or retrieving user information a synchronous call can help executing, processing and continuing the code with the retrieved data. Of course, the downside is that the main thread of your application is blocked. Applying this on our original code, we change it to
var r: TTMSFNCCloudBaseRequestResult; begin c.Request.Clear; c.Request.Host := 'https://httpbin.org'; c.Request.Method := rmGET; c.Request.Path := '/get'; Memo1.Lines.Add('Before executing request'); r := c.ExecuteRequest(nil, nil, false); if Assigned(r) and r.Success then Memo1.Lines.Add('Request finished'); Memo1.Lines.Add('Next Code'); end;
Before executing request Request finished Next Code
"Sync" in TMS WEB Core
As TTMSFNCCloudBase is cross-platform, executing REST request are also available in a TMS WEB Core application. The asynchronous implementation doesn't require any additional code, as by default, the code that runs in your browser runs asynchronously and the result is captured in a callback. Now, to make this a "synchronous" request, which in web terms means that the code will await the result in it's own scope, we'll need to apply the async/await methodology (https://javascript.info/async-await). Code that runs outside of the scope of the function that is awaiting the result will continue to run. This is a significant difference with all other platforms. When we have a piece of REST request code in a specific function/procedure and need to await the result of the request we need to add the [async] attribute to our function/procedure definition. For this sample, we added a button, which has a click event handler.
type TForm1 = class(TWebForm) WebButton1: TWebButton; [async] procedure WebButton1Click(Sender: TObject); private { Private declarations } c: TTMSFNCCloudBase; public { Public declarations } end;
procedure TForm1.WebButton1Click(Sender: TObject); var r: TTMSFNCCloudBaseRequestResult; begin c.Request.Clear; c.Request.Host := 'https://httpbin.org'; c.Request.Method := rmGET; c.Request.Path := '/get'; WebMemo1.Lines.Add('Before executing request'); r := await(TTMSFNCCloudBaseRequestResult, c.ExecuteRequest(nil, nil, false)); if Assigned(r) and r.Success then WebMemo1.Lines.Add('Request finished'); WebMemo1.Lines.Add('Next Code'); end;
Video
Below is a video explaining the difference between synchronous and asynchronous operations in TTMSFNCCloudBase, and how to execute simple GET requests.
Feedback
Although this might seem repetitive to see the keyword "Feedback" on every blog post, we greatly appreciate feedback. This only helps us improving our products. Want to know more about registring your own component based on TTMSFNCCloudBase, or even integrate authentication? Visit the following link to learn more: https://download.tmssoftware.com/doc/tmsfnccloudpack/gettingstarted/overview/
Pieter Scheldeman
Related Blog Posts
-
Diving deeper: Cloudbase REST in Delphi, Part 1/3: Basics
-
Diving deeper: Cloudbase REST in Delphi, Part 2/3: Extended
-
Diving deeper: Cloudbase REST in Delphi, Part 3/3: Sync vs Async
This blog post has received 2 comments.
But the await feature is for a TMS WEB Core project.
Bruno Fierens
All Blog Posts | Next Post | Previous Post
[dcc32 Error]: E2003 Undeclared identifier: ''await''
Brian Knudsen