Android Question run code only when all jobs completed successfully

Discussion in 'Android Questions' started by shb777, Feb 12, 2018.

Tags:
  1. shb777

    shb777 Active Member Licensed 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: Feb 12, 2018
  2. ronell

    ronell Well-Known Member Licensed User

    after a job has been executed , check the other 2 jobs
    Code:
    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
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    1. No need to use a timer.
    2. No need to handle JobDone.

    The simplest way is:
    Code:
    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: Feb 16, 2018
    Peter Simpson likes this.
  4. shb777

    shb777 Active Member Licensed User

    This is my implementation of you suggestion above, but none of the jobs are completing.


    Code:
    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
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    My mistake. It should be:
    Code:
    j.Initialize("", Me)
    Also add:
    Code:
    If j.Success Then
    '...
    Else
     
    Log("Error: " & j.ErrorMessage)
    End If
     
    Derek Johnson likes this.
  6. shb777

    shb777 Active Member Licensed User

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


    Code:
    If j.Success Then
               
                        StockQuote=j.GetString
     
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    Code:
    j.JobName
     
  8. DonManfred

    DonManfred Expert Licensed User

    not needed if you use wait for
     
    Peter Simpson likes this.
  9. shb777

    shb777 Active Member Licensed User

    j.jobname is blank, but i can figure it out by reading the data.
     
    Last edited: Feb 16, 2018
    Peter Simpson likes this.
  10. Peter Simpson

    Peter Simpson Expert Licensed 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: Feb 16, 2018
  11. shb777

    shb777 Active Member Licensed User

    so you have to set j.jobname yourself. it doesn't set it'self
     
  12. Peter Simpson

    Peter Simpson Expert Licensed 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.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Very simple and you don't need JobName:
    Code:
    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
     
    A Z M JANNAT UL KARIM likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice