Android Question How to completely run async operations? (and query a db)

jtare

Active Member
Licensed User
Longtime User
Which is the best solution to completely run async operations while querying a db? And leave the UI completely responsive.
The operation I need to run is a combination of querying db and very large for and while cycles.

I tried the asyncTask library but it crashes randomly, this is related with querying the db from the asyncTask_DoInBackground method. If it wasn't from this random crashes it would be the perfect solution, this library leave the UI 100% responsive

The "wait for" doesn't work, I use it like this post suggests, but still freezes the UI.

I can't paste here all the code, but it looks something like this:
(This code is just to get a rough idea on how the subs are distributed)

B4X:
'From the service module
Sub runThisCompletelyAsync(value As int,value2 As string,value3 As int)
   if MySub1(value) then
      'Do something
   else if MySub2(value2) then
      if value2 = "hello" then
         'Do something
         writeDB(value,value2)
      else
         MySub4(value)
      End if
   else
      Dim b As int = MySub3(value3)
      for i=0 to b
         'Do something
         'Read file from DirInternal
      next
      writeDB(value,value2)
   end if
End Sub

Sub MySub1(val As int) As boolean
   'Query db synchronously
End Sub

Sub MySub2(val As string) As boolean
   'Query db synchronously
End Sub

Sub MySub3(val As int) As int
   'Query db synchronously
End Sub

Sub MySub4(val As int)
   for i=0 to val
      'Do something
   next
End Sub

Sub writeDB(val As int,val2 As string)
'Write DB async
End Sub

The "runThisCompletelyAsync" doesn't change the UI, it rather write or not the db, but in the process it query the db, read files and run very long for cycles.

Does someone have an idea on how to perform this operation completely async?
 

edgar_ortiz

Active Member
Licensed User
Longtime User
Hi,

I define a global variable (Glb_IO_Complete) in Starter Service and set in the "sub" that access the server (Database ) and check the value of Glb_IO_Complete in the activities.

Regards,

Edgar
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
'Query db synchronously
If you run your queries synchronously, then wrapping this in a wait for does not suddenly make it async. Use asynchronous DB methods, otherwise, deal with the issues that synchronous DB methods have.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

jtare

Active Member
Licensed User
Longtime User
you run your queries synchronously
Yes, I run this queries synchronously because they run really fast.
What freezes the UI are the long for loops.

I think the solution is what I was trying to avoid, completely restructure the code, run the queries asynchronously although they run super fast and execute the "for" loops under this method:

https://www.b4x.com/android/forum/t...hat-return-values-resumablesub.82670/#content

I haven't had success using the "wait for" on normal subs, only experienced wait for's in sql. Later I'll update my results.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What freezes the UI are the long for cycles
Put a sleep(x) in the for loops and play with the x. That will make your method runThisCompletelyAsync resumable, so you may have to change the way you call it if you expect it to finish all before returning (use a wait for/complete).

Note: That should "unfreeze" the UI.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Upvote 0

jtare

Active Member
Licensed User
Longtime User
Why do you have to process so many data? / why so complicated? / why do you need asynch processing at all? (I have db's with millions of rows and the response is < 0.05 secs!).

I think I explanied myself wrong. My query times are super fast too, the issue is that depending of what the query returns I need to perform very long "for loops" and "do until loops", this loops are the ones that freeze the UI.

in a service module:
(NEW DATA) + (QUERY DB + LONG "for and do until loops")-----> (WRITE OR NOT THE NEW DATA)

Inside this loops I create lists, compare strings and look for images files (dir internal) that meet the requierment. (time can go from a few milliseconds to 2-3 seconds).

Put a sleep(x)
You are right, I tried it and it does unfreeze the UI, but is a very laggy UI.

What have worked the best is the AsyncTask library, but it crashes because of querying the db. What I will try next is to query async and after having all the results, I'll run the asyncTask_backaground and pass all the data, hopefully it won't crash since the query happens outside this library.
 
Upvote 0
Top