Android Question how correct to do long operation in the main thread ?

peacemaker

Expert
Licensed User
Longtime User
In some database init must be done first of all, other operations are not allowed - so, the process can be done as is in the main thread, with some progressbar.
Init is a process with a huge text file (20000 ... 100000 lines, several megabytes, or tens of), reading line by line.

I usually make using Doevents in some IF condition. But here is very long operation, maybe 5 minutes. And an user may need to interrupt it, seeing the progressbar percent.

How correct to do such loop with minimal delay ?
 

udg

Expert
Licensed User
Longtime User
Did you try something like:
1. read 200 lines
2. parse/regex lines
3. batch insert lines
4. update percentage (or any other user feedback item)
5. repeat the process with next 200 lines until input data is done
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

udg

Expert
Licensed User
Longtime User
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But i posted above already - adding requests one by one into a batch needs almost the same time. But reading\parsing and applying the batch into the db - is much faster operations.... :(
Seems, it's SQLite limitation ?

But:
I used it on importing a text file 14 MB in size with 175000 lines into a table on a Galaxy Tab A tablet. It worked.
16 sec on galaxy tab A tablet.
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Instead of a loop, which potentially freezes the UI, you could use a timer.

something like this in the tick event can replace a for-next :

i must be made global so it keeps its value between ticks.

B4X:
If i < maxvalue then
'Do what you need to do at each cycle
i = i + 1 'step
else
Timer1.Enabled = False
i = 0
end if
 
Last edited:
Upvote 0
Top