Android Question run code only when all jobs completed successfully

shb777

Active Member
Licensed User
Longtime User
I'm using OkHttp library and running three jobs, Job1, job2 and job3. I have code i only want to run when all three jobs have completed successfully. Where do I put that? The only way I can think of doing it, is start a timer that keeps checking until all three are true. I Wonder if there's an easier way.
 
Last edited:

ronell

Well-Known Member
Licensed User
Longtime User
after a job has been executed , check the other 2 jobs
B4X:
Sub JobDone (Job As HttpJob)

    ProgressDialogHide
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        
Select Job.JobName
    
    Case "job1"
        
        'check job2 and job3
        
    Case "job2"
        
        'check job1 and job3
    Case "job3"
        
        'check job1 and job2
        
End Select

    Job.release

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. No need to use a timer.
2. No need to handle JobDone.

The simplest way is:
B4X:
Dim success As Boolean = True
For Each url As String In Array("url1", "url2", "url3")
 Dim j As HttpJob
 j.Initialize("", Me)
 j.Download(url)
 Wait For (j) JobDone(j As HttpJob)
 Success = Success AND j.Sucess
 If j.Success Then
 '...
 End If
 j.Release
 If Success = False Then Exit
Next
[code]
 
Last edited:
Upvote 0

shb777

Active Member
Licensed User
Longtime User
This is my implementation of you suggestion above, but none of the jobs are completing.


B4X:
Dim success As Boolean = True


    For Each url As String In Array("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=GOOGL&interval=5min&apikey=ME7KA1AZTBN2K5UA","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AMZN&interval=5min&apikey=ME7KA1AZTBN2K5UA")
 
 
 Dim j As HttpJob
 j.Initialize(Me, "")
 j.Download(url)
 Wait For (j) JobDone(j As HttpJob)
        success = success And j.Success
        If j.Success Then
        
                    StockQuote=j.GetString
                    TempStr=GetStockPrice("hhh",6,LabelGOOGLPrice)
                
                    Log(TempStr)
            
                    StockQuote=j.GetString
                    TempStr=GetStockPrice("hhh",6,LabelAMZNPrice)
                
                    Log(TempStr)
                
        
    
 '...
 End If
 j.Release
 If success = False Then Exit
Next
'[code]


End Sub
 
Upvote 0

shb777

Active Member
Licensed User
Longtime User
Now it's working, but after success, how do you know which url the j.GetString is from?


B4X:
        If j.Success Then
           
                    StockQuote=j.GetString
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
B4X:
     j.JobName
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I personally always put a name in j.jobname = "whatever" per job, especially for multiple download. But hey that's just my personal preference rightly or wrongly, but my multiple downloads can run into multiple numbers. j.jobname is not really needed, but I like it.

Anyway @shb777 watch this video from @Erel vimeo.com/255570732, it's seriously interesting and extremely informative...
 
Last edited:
Upvote 0

shb777

Active Member
Licensed User
Longtime User
I personally always put a name in j.jobname = "whatever" per job, especially for multiple download. But hey that's just my personal preference rightly or wrongly, but my multiple downloads can run into multiple numbers. j.jobname is not really needed, but I like it.

Anyway @shb777 watch this video from @Erel vimeo.com/255570732, it's seriously interesting and extremely informative...
so you have to set j.jobname yourself. it doesn't set it'self
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
No it doesn't, but as @DonManfred said previously, you don't really need it. My habit is to use it, especially is I usually jump out and back into the main URL sub after wait for, so the sub I'm jumping into knows the job name of the information coming into it. But that's not really needed, it's just a personal option that I prefer to do plus I use it for log purposes, but that's just me.

You should watch he video that I linked above.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Now it's working, but after success, how do you know which url the j.GetString is from?
Very simple and you don't need JobName:
B4X:
Dim success As Boolean = True
For Each url As String In Array("url1", "url2", "url3")
 Dim j As HttpJob
 j.Initialize("", Me)
 j.Download(url)
 Wait For (j) JobDone(j As HttpJob)
 Success = Success AND j.Sucess
 If j.Success Then
 '...
 Log("Url downloaded: " & url) '<--------- use the url variable
 End If
 j.Release
 If Success = False Then Exit
Next
 
Upvote 0
Top