8

I'm building an Firemonkey app in Rad Studio XE7 where on single button click, I will need to do multiple( around 7) web service calls using TRestRequest. Every web service will return json object which will then populate dataset. I'm looking for a way to these calls concurrently and not have UI of the app to lock.

Which way do you recommend to do this? I saw that Embarcadero introduced new Task and Feature functionality for threading, but I'm still not sure if we can use that and how. Also, I saw that there is a function to execute TRestRequest asynchronously, using this function:

function TCustomRESTRequest.ExecuteAsync(ACompletionHandler: TCompletionHandler = nil; ASynchronized: boolean = true; AFreeThread: boolean = true): TRESTExecutionThread;

but I can't find any documentation on how to use this method and what it does.

Any input will be greatly appreciated.

6
  • @JerryDodge doesn't Async imply it will not block? Eventually the async operation will terminate and the app can do something with the result.
    – mjn
    Dec 3, 2014 at 19:31
  • @mjn This one button click where we will call these 7 web services will actually open a multiview which in our case will contain a bunch of filtering comboboxes, which will be populated by getting data from web services. My idea was to give ability to user to actually cancel this filtering and just go back by touching area outside of multiview which will close the multiview and not apply any filters. Idea for multiple parallel requests was that we think that those 7 comboboxes will get populated faster if we send 7 requests in parallel instead of sending one request at a time.
    – Emulic
    Dec 3, 2014 at 21:36
  • @JerryDodge You should execute all long(er) running tasks in background to prevent the UI thread from blocking. This is not only for the user but also the OS is looking for not responding apps.
    – Sir Rufo
    Dec 3, 2014 at 23:29
  • @JerryDodge f.i. Android will popup a dialog if an app seems to be freezed, because of not responding. So it is a must to do so. Sorry, I cannot get your point for asking why. Feels to me like asking for "Why do you want not to drive through neighbors garden?" :o)
    – Sir Rufo
    Dec 3, 2014 at 23:40
  • @JerryDodge It is for sure the right choice - without any acceptable well working option
    – Sir Rufo
    Dec 3, 2014 at 23:52

1 Answer 1

2

The ExecuteAsync(ACompletionHandler: TCompletionHandler = nil; ASynchronized: boolean = true; AFreeThread: boolean = true) method is indeed the way to go. As the name implies, it is asynchronous; meaning your program remains responsive after firing the request, or even after firing several requests.
However, it is important that you fire these different requests from different object instances; if you fire an ExecuteAsync from a TRESTRequest instance that is already performing an ExecuteAsync, the new request will get in the way of the existing request. You must create a separate TRESTRequest instance for each parallel call.

Note that its first parameter is a procedure; you pass a procedure of your choice as that parameter. The only requirement is that the procedure has the right signature; in this case, that it is a procedure that has no parameters.

The ExecuteAsync method is defined in REST.Client. (I've got Delphi XE-10.1. Berlin so it has one extra parameter, ACompletionHandlerWithError - which is called on error. The principle remains the same though).

Let's have a look:

function TCustomRESTRequest.ExecuteAsync(ACompletionHandler: TCompletionHandler = nil; ASynchronized: boolean = true;
  AFreeThread: boolean = true; ACompletionHandlerWithError: TCompletionHandlerWithError = nil): TRESTExecutionThread;
begin
  Result := TRESTExecutionThread.Create(Execute, self, ACompletionHandler, ASynchronized, AFreeThread, ACompletionHandlerWithError);
end;

What happens here is that a new thread is created in which the REST Request is executed. If the response comes in, it is handled by ACompletionHandler.
By default, ACompletionHandler runs on the new thread created by ExecuteAsync. If you want it to run on the main thread, you should set ASynchronized to true.

But how do you get access to that response, and make it accessible to the rest of your program?

FireMonkey's TRESTRequest class has a property Response, that refers to the TRESTResponse object that contains the server's response to our request.
Unfortunately, neither the TRESTRequest nor the TRESTResponse object are passed to our CompletionHandler!

So we need to somehow give the CompletionHandler this information. Fortunately, we can use a method as a CompletionHandler.

Let's assume that the class that is going to process the resulting data is called DataOwner. Our purpose is that DataOwner has access to the TRESTResponse instance associated with our TRESTRequest instance.
The easiest way to do this is to make TRESTRequest and/or TRESTResponse a member of DataOwner.

Assume your request was fired from a TRESTRequest instance named MyRESTRequest and processed by a function processResponse, you would use the following code:

type TDataOwner = class
   MyData: TSomeDataType;
   procedure GetData();
   procedure FillDataSet();
end;

implementation

procedure DataOwner.GetData();
begin
  // ... initialize MyRESTRequest here...
  MyRESTRequest.ExecuteAsync(FillDataSet);
end;

procedure DataOwner.FillDataSet();
begin
    MyData := processResponse(MyRESTRequest.Response);
end;

If you want to fire several requests in parallel, you need to use this pattern several times. It is unfortunate that we don't have direct access to the TRESTRequest instance or TRESTResponse instance in our Completion Handlers, because this means we have to do the bookkeeping ourselves. In other words, it is up to the programmer to make sure that the Completion Handler processes the right Response object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.