Android Question Getting httpjobs to wait for previous httpjob to complete

Peter Webb

Member
Licensed User
Hi,

I have a problem with executing httpjobs and waiting for them to complete before the next httpjob is executed.

For eg.

1) I query a remote mysql server to get the total records on a database there
2) I then insert x amount of additional records to that remote mysql server
3) I then requery the remote mysql server for another record count.

I have been using the JobDone sub and trying to make it wait for a previous httpjob to complete before the next one is executed. However, this doesnt seem to work correctly.

Has someone got a code example of how to make the httpjobs execute completely in the order required?

Any help would be appreciated.

Regards
 

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
I use a list to add the diferents jobs to do and a flag "waiting" as boolean .

When http request , waiting = true, when job done. Waiting = false and delete thist job from the list.

And a timer that is active until job list is empty .
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need a timer here.

You can add all the jobs to a global list and send the first one.
Remove the first item in the list in JobDone and send the next one.
B4X:
Sub JobDone (job As HttpJob)
 ...
 ListOfJobs.RemoveAt(0)
 If ListOfJobs.Size > 0 Then
  Dim j As HttpJob = ListOfJobs.Get(0)
  j.Download(...)
 Else
  Log("Finished all jobs!")
 End If
job.Release
End Sub

The other option is to create the next job in JobDone and send it. Use a different job name to distinguish between the different jobs.
 
Upvote 0

Peter Webb

Member
Licensed User
Hi Erel,

Thanks for the reply.

I think I almost understand your solution. Do you have a more detailed sample that includes how to add jobs to the queue and what needs to be put in the j.Download( section plus anything else that belongs in the JobDone?

Thanks,
 
Upvote 0

Peter Webb

Member
Licensed User
Hi Erel,

I have cases where I would need the results from an earlier request to make additional queries and other times I just need to execute particular queries in order.

Regards
 
Upvote 0

Peter Webb

Member
Licensed User
Thx for the reply.

I am still missing something here. Lets try the simplest example.

for eg
I have x httpjobs to execute.
Each one must finish before the next one can continue.
What is the code logic i would need for this?

Regards
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
It's easier than you think. Let's say you want to start 3 Jobs in a sequence: J1, J2 and J3.

Just start the first Job J1 wherever you want.

B4X:
Sub JobDone (job As HttpJob)
If Job.Name = "J1" Then
  If job.Success Then
  Dim J2 As HttpJob
  'prepare new job and send it
End If

If Job.Name = "J2" Then
  If job.Success Then
  Dim J3 As HttpJob
  'prepare new job and send it
End If

If Job.Name = "J3" Then
  If job.Success Then
   Log("all three jobs are done...")  
End If

End If

In JobDone you check which job came back. Here it is J1. If it was successful then start J2 FROM HERE and J3, too.

Just play with it. It's good to learn such things.

PS: When I was new to HttpUtils I had the same questions

PS2: There are a lot of ways to do it (see Erel's example. He uses a List and removes the finished jobs from it).
 
Upvote 0

Peter Webb

Member
Licensed User
Hi KMatle,

Thanks for the reply. That helps a lot.

I am also interested in Erels method of adding jobs to the list and them removing them as they are completed. The only problem with Erels solution was I have no idea of how to add jobs to the list and i was not sure to put in the line where he has j.Download(...)

Many thanks,

Peter Webb
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…