method DownloadData;
begin
var lResponse := await Http.ExecuteRequestAsJson(new HttpRequest(URL));
if lassigned(aResponse.Content) then begin
await dispatch_async(dispatch_get_main_queue());
fData := lResponse.Content;
tableView.reloadData;
end;
end;
When I’m using await in .net the method DownloadData would return a Task. Why doesn’t the code in this example do the same ?
Mainly because task is a .NET specific API, while this sample uses Elements RTL, which is cross-platform and not using then general async/await infrastructure of .NET. The whole point fo awaitable closures is to be able to see await with APIs that don’t already support Task.
Oh. Would it be possible to add that kind of support in a cross platform manner ?
I have some code in an iOS app that makes multiple http requests and then waits for all to finish before doing something else. Currently I’m using NSOperationQueue but I think I would prefer to use Tasks.
Well, this is a different paradigm, and especially designed for methods that take a callback. The point ti that fete (say on Cocoa oa Java) these APIs aren’t really ours to redesign toms Task (if we wanted to). This feature lets you use await on the even though they don’t use Task — that’s the whole point — and they’ll use the callback to execute the remaining code. Think if it as the remainder of the method body/scope being moved into the closure.