Jump to content
saeedbay

ProgressBar not update in android

Recommended Posts

i'm using a while  loop for transfer sql server records on server to sqlite db on android device

i'm using progress bar to showing progress of doing work but progress bar not update in while loop 

Share this post


Link to post

You need to know that the SQL engine should have a "like a Callback function" so that you can interact with the data manipulation (records being manipulated). For example, with each record manipulated, "your function" would be called to execute another conditional task.


On the other hand, on mobile, you should work with "threads" in the background so that the main interface remains completely free for important system tasks.
Thus, you could try to create a "thread" that every moment, check what has already been done, and then update your UI. Preferably at intervals that are not very close, for example, every 5%, 10%, etc...

 

you code is?

Share this post


Link to post

i have used threads before but it didnt work and i had to delete it

 my current code is :

 

 

procedure TSettingF.UpdateStoreFromServer;
var count,i,StoresCount,BPrice:integer;
  step:Real;
  BoughtPrice:Double;
  fieldNames1,fieldNames2,UpdFieldNames:String;

begin
ProgressBar1.Visible:=True;
ProgressBar1.Max:=100;
ProgressBar1.Value:=0;
Progress:=0;

lblLastUpdate.Text:='please wait...';
lblLastUpdate.Repaint;

RunMSQuery('Select ID,TITLE,BOUTH_PRICE,SELL_PRICE,UNIT'+fieldNames1+' from Stor WITH(NOLOCK)  ',DataM.MSStorQry);
count:=DataM.MSStorQry.RecordCount;
if count>0 then
  step:=100/count;
i:=0;
ExecFDQuery('DELETE FROM STOR WHERE (1=1) ',DataM.FDStorQry);

while not DataM.MSStorQry.Eof do
    begin
    try
     DataM.FDStorQry.Close;
     DataM.FDStorQry.SQL.Clear;
     DataM.FDStorQry.SQL.Add('INSERT INTO Stor (ID, TITLE, BOUTH_PRICE, SELL_PRICE, UNIT'+fieldNames1+')VALUES '      +'('+DataM.MSStorQry.FieldByName('ID').AsString+','+QuotedStr(DataM.MSStorQry.FieldByName('TITLE').AsString)+','+FloatToStr(BoughtPrice)+','+FloatToStr(DataM.MSStorQry.FieldByName('SELL_PRICE').AsFloat)+','+QuotedStr(DataM.MSStorQry.FieldByName('UNIT').AsString)+','+FloatToStr(DataM.MSStorQry.FieldByName('STOCK00').AsFloat)+UpdFieldNames+')');
     DataM.FDStorQry.ExecSQL;
     except
        on e: Exception do
        begin
          ShowMessage(e.Message);
        end;
     end;
     DataM.MSStorQry.Next;
     inc(i,1);
     Progress:=Round(i*step);
     if ProgressBar1.Value<>Progress then
        begin
//         Sleep(10);
           ProgressBar1.Value:=Progress;
    //     Application.ProcessMessages;
        end;
    end;

end;

Share this post


Link to post

First, DB actions + Thread is a complicated subject!

 

DB it does not provide a "feedback" to the user (developer) so that he can follow the current process... Therefore, such actions are very fast, as there is no update on the screen.
On the other hand, this would not be desired, since the purpose of SQL actions is to promote "task execution" and not "task follow-up".

 

Second, as the actions in a DB are performed in a secondary plane (out of the user's reach), using a thread can be complicated due to the non-synchronism between the two actions: doing something in the DB and executing the thread.

 

So, what you could do is "give the impression that the progressbar is being fed by the SQL responses", that is, do a "fake-updates on the progressbar".

 

NOTE: all updates must be done using "Synchronize" or "Queue" procedure of thread!

Share this post


Link to post
  • Create a shared percentage variable that can be referenced from both the db thread and the main thread
  • In the UI, show the progress bar display, and refresh it on a timer f.x every 500ms, getting the percentage from the shared variable
  • Create a thread to do the SQL fetch/write operations and update the shared progress percentage variable
  • Once the thread is completed, close the progress bar display

You may want to protect the shared variable with a critical section.

Share this post


Link to post

Also, I would like to stress that doing direct SQL to a server from a mobile device is high risk.

I recommend a REST service as a front end to the database.

  • Like 1

Share this post


Link to post
14 hours ago, Lars Fosdal said:

Also, I would like to stress that doing direct SQL to a server from a mobile device is high risk.

I recommend a REST service as a front end to the database.

thanx

but I don't know much about it

can u write simple sample for my code so I know how to do this

Share this post


Link to post

Have a look at 
C:\Users\Public\Documents\Embarcadero\Studio\22.0\Samples\Object Pascal\Database\RESTDemo

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×