我已经宣布了这些新类型:
type
TRacer = record
name: string;
win, tie, loss, points: integer;
end;
TRacersList = TList<TRacer>;
我在数据模块中有一个程序如下:
procedure DMConnection.getDivision(aList: TRacersList; const game, id: string);
begin
//1) setup the REST params
RESTRequest.Params[0].Value := game;
RESTRequest.Params[1].Value := id;
//2) execute async so UI won't freeze
RESTRequest.ExecuteAsync(procedure
var tmp: TRacer;
ja: TJSONArray;
jv: TJSONValue;
begin
//parse the JSON. The RESTResponse is a TRESTResponse that holds the result of the RESTRequest. I have made a test inside the component (right click -> execute) and it works properly (= I can see the JSON inside the Content property).
ja := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;
for jv in ja do begin
tmp.name := jv.GetValue<string>('name');
tmp.win := jv.GetValue<integer>('Wins');
tmp.tie := jv.GetValue<integer>('Ties');
tmp.loss := jv.GetValue<integer>('Loss');
tmp.points := jv.GetValue<integer>('Pts');
aList.Add(tmp);
end;
end);
end;
如果您想知道,这是JSON字符串:
[{"name":"payer1","Wins":"1","Ties":"0","Loss":"0","Pts":"3"},{"name":"Velocity","Wins":"0","Ties":"0","Loss":"1","Pts":"0"},{"name":"test2","Wins":"0","Ties":"0","Loss":"0","Pts":"0"}]
在主窗体中,我能够以这种方式调用该过程:
DMConnection.getDivision(myList, 'mku', 'k2');
我有以下问题。变量myList: TRacersList
在表单的OnCreate和OnDestroy事件中创建并释放。如果您尝试运行此代码:
DMConnection.getDivision(myList, 'mku', 'k2');
ShowMessage(myList.Count.toString);
结果为0.相反,如果我将aList.Add(tmp);
OUTSIDE 设置为异步请求(例如,在参数设置之前),则计数为1.
这意味着当列表位于ExecuteAsync
的完成处理程序之外时,该列表正在填充,但是当列表位于其中时,它不会被填充。有什么想法吗?
我已经知道,默认情况下,Synchronize设置为True,因此添加
TTHread.Synchronize(nil, procedure
begin
aList.Add(tmp);
end)
没用。我该怎么办?我希望在数据模块中有这个解析/休息的东西,这样我就可以将互联网内容与我的app实现分开。