1
In a mobile project I have a code snippet that fills a Listview with data from a Dataset (I’m not using Livebindings). Up to a certain point everything was working fine until I decided to add an animation while loading the list and put the loading section inside a thread as shown below. The fact is that the load has become unstable because at some times it runs normal and at others it partially executes and the debug returns the error "Raised Exception class Segmentation fault (11)". Here is the code:
procedure TViewPesquisaCliente.PreencherListaClientes;
var
oItem: TListViewItem;
oId, oCodigo, oNome, oCPF: TListItemText;
oClientes: TDataSet;
begin
TLoading.Show(ViewPesquisaCliente, 'Carregando lista de clientes...');
TThread.CreateAnonymousThread(
procedure
begin
try
oClientes := TControllerCliente.New.Clientes;
lstLista.Items.Clear;
lstLista.BeginUpdate;
oClientes.Open;
while not oClientes.Eof do
begin
oItem := lstLista.Items.Add;
oId := TListItemText(oItem.Objects.FindDrawable('Id'));
oCodigo := TListItemText(oItem.Objects.FindDrawable('Codigo'));
oNome := TListItemText(oItem.Objects.FindDrawable('Nome'));
oCPF := TListItemText(oItem.Objects.FindDrawable('CPF'));
oId.Text := oClientes.FieldByName('Id').AsString;
oCodigo.Text := oClientes.FieldByName('Codigo').AsString;
oNome.Text := oClientes.FieldByName('Nome').AsString;
oCPF.Text := oClientes.FieldByName('CPF').AsString;
oClientes.Next;
end;
finally
TThread.Synchronize(nil,
procedure
begin
TLoading.Hide;
end);
lstLista.EndUpdate;
oClientes.Close;
oClientes.DisposeOf;
end;
end
).Start;
end;
The Tloading.Show section loads the animation from the screen before starting the list and Tloading.Hide closes the animation. I have no experience with threads and according to some research I read the problem may be in trying to manipulate the screen objects within the thread. I even commented the excerpt from the line "oId.Text := " to "oCPF.Text := " and it’s working again, but Listview is empty, so I have this problem. Someone would have a suggestion?
When I did a loading... I used the
timer
find it easier.– David