Android Question B4X do something if Wait For takes long

Hello everyone!

I have created an app that akes lot's of API Calls. Some times the connection to the server isn't easy, so the app has to wait. This is why I have added a Loading Indicator for the user to know that everything works fine, they just have to wait a few seconds.

Nevertheless, the way I 've written it, the indicator appears in every API Call (and dozens of them are happening in this app) for a short period.

I wand it to appear only if the wait lasts more than 1 or 2 seconds.

Wait For With Loading Indicator:
Dim j As HttpJob

j.Initialize("", Me)

LoadIndicatorTabReturn.Show

j.PostString(link, m.As(JSON).ToCompactString)
j.GetRequest.SetHeader("Authorization", "Bearer " & token)
j.GetRequest.SetContentType("application/json")

Wait For (j) JobDone (j As HttpJob)

LoadIndicatorTabReturn.Hide

Is this possible? Thank you in advance.
 

agraham

Expert
Licensed User
Longtime User
Don't use the library, use Erels' post
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Rem out the line "LoadIndicatorTabReturn.Show" so it wont display the indicator (we'll do it elsewhere).

Then, right before calling the httpjob, start a timer (timer.enabled = true) that will trigger in 1000ms (1 sec).

Then in the timer_tick routine add these lines:

Timer.Enabled = False
LoadIndicatorTabReturn.Show


Then after the wait for line, have these lines:

Timer.enabled = false 'this disables the timer in case the timer_tick sub didn't have a chance to tick yet
LoadIndicatorTabReturn.Hide 'hides the indicator

So, when the httpjob is called, the timer is also started, and if the httpjob doesn't finish within 1 second, the timer_tick sub is executed and the indicator is shown and the timer is disabled so the tick sub wont trigger again.

Then, when the http job finishes, it will disable the timer as a backup in case the httpjob took less than 1 second to complete and the timer_tick event never ran.

This way, if the httpjob process executes in less than a second, the indicator will never be shown.
 
Last edited:
Upvote 0
Top