Delphi and Threads

  

I have a TListView filled from a TRestRequest with TThread.CreateAnonymousThread.
Everything works OK on the first run, but on PulltoRefresh it is not working (no Freezing, no Results). If I don’t use Threads, it works fine with freezing.
Where is the problem when I use the same code except the ListView.Items.Clear?
Here is my code.
procedure TForm4.Server;
var
i, Id: Integer;
UName, Descr,ThumbUrl: UnicodeString;
LItem: TListViewItem;
begin
FDMemTable1.EmptyDataSet;
TThread.CreateAnonymousThread(
procedure
begin
RESTClient1.BaseURL := {my PHP URL};
RESTRequest1.Execute;
FDMemTable1.First;

TThread.Synchronize(nil,
procedure
var
i: integer;
begin
for i := 0 to FDMemTable1.RecordCount – 1 do
begin
Id := FDMemTable1.FieldByName(‘id’).Value;
UName := FDMemTable1.FieldByName(‘username’).Value;
Descr := FDMemTable1.FieldByName(‘descr’).Value;
ThumbUrl := FDMemTable1.FieldByName(‘image_url’).Value;
ListView1.BeginUpdate;
LItem := ListView1.Items.Add;
LItem.Data[‘Id’] := Id;
LItem.Data[‘UName’] := UName;
LItem.Data[‘Descr’] := Descr;
LItem.Data[‘ThumbUrl’] := ThumbUrl;
LItem.Adapter.ResetView(LItem);
ListView1.EndUpdate;
FDMemTable1.Next;
end;
end
);
end
).Start;
end;

procedure TForm4.FormShow(Sender: TObject);
begin
LoadFormState;
Server;
end;

procedure TForm4.ListView1PullRefresh(Sender: TObject);
begin
ListView1.BeginUpdate;
try
ListView1.Items.Clear;
finally
ListView1.EndUpdate;
end;
Server;
end;

I have to mention the items created on ListView1UpdatingObjects. I test it on Android.

Comments are closed.