Preventing XDATA server from exiting before CALLBACK code executes

I implemented geocoding using a TMSFNCGeocoder in one of our XCODE servers. To prevent the server from exiting before the callback completed, I used the code shown below. Is there a better way to handle this?

ExitOk := False;
Geocoder.GetGeocoding(p_Address,
  procedure (const ARequest: TTMSFNCGeocodingRequest;
  const ARequestResult: TTMSFNCCloudBaseRequestResult)
  var
    I: Integer;
  begin
    if ARequest.Items.Count = 0 then
      ExitOk := True
    else
      begin
        for I := 0 to ARequest.Items.Count - 1 do
        begin
          //...code removed
        end;
        ExitOk := True;
      end;
  end);
Ticks := 0;
while not ExitOk do
begin
  Ticks := Ticks+1;
  if Ticks > 100 then
    ExitOk := True
  else
    Sleep(100);
end;

I suggest implementing the service method to either launch the geocoding process and return ‘still being determined’ or the location that has previously been determined.

Waiting for a hard-coded time will not work reliably.
The client can then repeat the service call a number of times.

On the server side you can also implement a mechanism to know if the geocoding failed, is still going on, or has completed.

The client having to ask more than once is fine.

I just implemented it using the Google geocoder.

This is the reply from the server for the first request:

{
  "$id": 1,
  "IsRunning": true,
  "IsError": false,
  "Longitude": 0,
  "Latitude": 0
}

And if you repeat the same request "a bit later" from the client, the result will change to:

{
  "$id": 1,
  "IsRunning": false,
  "IsError": false,
  "Longitude": -77.0365298,
  "Latitude": 38.8976763
}

I will have to do some cleanup, but I might publish it as an example for asynchronous methods inside XData server methods.

In my application, the client would not need to check back with the server. The server associates each geocoding request with a record in our database. The server just needs to send the client the RequestID for that record.

The server will update the "GeocodeJSON", "Status", and "CompletionStamp" fields when the CALLBACK routine executes. The client just has to wait for the status on the request record to change to "DONE" or for the process to timeout.

I have a concern with the call to Geocoder.GetGeocoding. The XDATA server is on our internal network so it will be accessible even if the internet is down. What happens if the internet geocoding service is inaccessible at the call? What happens if the internet goes down while the callback is waiting for a response?

I implemented a version without the ExitOk loop. When the procedure is exited without the loop, the callback routine never executes.

You will need to adapt your architecture accordingly for the geocoding instance to be hosted across multiple service calls. Same for the information about the status of the service call.

Can you let me know when you publish your example?

Please contact me via email to discuss the implementation. I can offer you individual support if you are interested as a certified consulting partner (https://www.tmssoftware.com/site/partners.asp).