Android Question Wait for... waits?

LucaMs

Expert
Licensed User
Longtime User
upload_2017-10-17_7-4-27.png


The Test project works: the file (sqlite DB) is saved and it is then accessible.

Real project log (shortly):
Service Create
Service Start
Wait For SubName

Activity Main Create
Activity Main Resume

** Service httputils2 Create
** Service httputils2 Start
** Service httputils2 Start
List of files received.

Downloading...
Activity Main Pause
' other "empty" Activities start
Saving file on device

Saved.
Initialize the db file:
crash -
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database



Wait For (SubName...) seems to not wait, correct?


 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several mistakes in this code:
1. If you want to wait for a sub to complete then you should make it return a ResumableSub object and use this syntax:
B4X:
Wait For (HttpJobSim1) Complete(Result As Object) 'you can change the result type
[B4X] Resumable subs that return values (ResumableSub)

2. Wait For doesn't block the program flow. Wait For is equivalent to Return where the sub will be resumed at a later point: [B4X] Resumable Subs - Sleep / Wait For
This means that Activity_Create will be executed before Service_Create is resumed and completed.

If you want that Activity_Create will wait for the resumable Service_Create to complete then you will need to change Activity_Create to:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     Wait For Service_Complete
   End If
   Log("Starter.MyVar = " & Starter.MyVar)
End Sub

'and service create:
Sub Service_Create
   Wait For (MyRoutine) Complete (Result As Int)
   MyVar = Result
   CallSubDelayed(Main, "Service_Complete")
End Sub

I think that it is a mistake to wait for the httpjobs to complete before you load the layout. A better approach is to use CallSubDelayed to call a different sub in the activity.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
There are several mistakes in this code:
1. If you want to wait for a sub to complete then you should make it return a ResumableSub object and use this syntax:
Watch line 17 in Starter:

Wait For (MyRoutine) Complete (Result As Int)

I have not used the same for the two "simulated" http jobs, only because it is an example ;).


2. Wait For doesn't block the program flow. Wait For is equivalent to Return where the sub will be resumed at a later point: [B4X] Resumable Subs - Sleep / Wait For
This means that Activity_Create will be executed before Service_Create is resumed and completed.
Yes, I remembered this ... afterwards :D
Its name is a bit misleading; it waits without waiting :), the flow returns to the sub which contains the Wait For when the "waited" routine is completed.


If you want that Activity_Create will wait for the resumable Service_Create to complete then you will need to change Activity_Create to:
Thank you for this example.


I think that it is a mistake to wait for the httpjobs to complete before you load the layout. A better approach is to use CallSubDelayed to call a different sub in the activity.
As you wrote elsewhere, the Starter Service is the best place to initialize a global DB, so I tried that code (#1).



Thank you.
 
Upvote 0

Dey

Active Member
Licensed User
Longtime User
There are several mistakes in this code:
1. If you want to wait for a sub to complete then you should make it return a ResumableSub object and use this syntax:
B4X:
Wait For (HttpJobSim1) Complete(Result As Object) 'you can change the result type
[B4X] Resumable subs that return values (ResumableSub)

2. Wait For doesn't block the program flow. Wait For is equivalent to Return where the sub will be resumed at a later point: [B4X] Resumable Subs - Sleep / Wait For
This means that Activity_Create will be executed before Service_Create is resumed and completed.

If you want that Activity_Create will wait for the resumable Service_Create to complete then you will need to change Activity_Create to:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     Wait For Service_Complete
   End If
   Log("Starter.MyVar = " & Starter.MyVar)
End Sub

'and service create:
Sub Service_Create
   Wait For (MyRoutine) Complete (Result As Int)
   MyVar = Result
   CallSubDelayed(Main, "Service_Complete")
End Sub

I think that it is a mistake to wait for the httpjobs to complete before you load the layout. A better approach is to use CallSubDelayed to call a different sub in the activity.

Just to say does not center anything with the thread.

Thanks Erel is the most important feature you have entered
Wait For ResumableSub

I have rewritten all DB JRDC2 management and finally I have the situation in hand!

You just have to do a try to figure out how it works and work gets a lot easier!
Thanks again
 
Upvote 0
Top