HttpUtils2, jobs, services, and timming

JTmartins

Active Member
Licensed User
Longtime User
I would like some ideas of how to handle this kind of situations.

I run a few SQL queries to a remote server. (this is part of a sync routine)

The values that result from this queries are needed for the rest of the app to continue.

I've tried to run the queries in the activity itself or as a service.

Some time works perfectly but occasionally the program flow continues and when the data is needed, it still is not available (although it comes up a little later)...but then...its too late :)

So...I need to wait, to ensure that all queries have been processed and all jobs terminated. Something like raising an event and only then the app is ready to continue as I know that without the data from the queries it will faill.

I also tried CallSubDelayed...but occasionally the same kind of problem happens. It seems to depend if the server is a little busier or data amount is higher, etc.

I know that some people had issues with this kind of things, and I'm still extremely confused on the approach to handle this kind of situations.

All help will be welcomed. Some sort of tutorial or whatever you guys can throw me so that I can understand how to handle this kind of situations.

many thanks
José


By the way, I also tryed the idea of
B4X:
Select Job.JobName
 Case "JobA"
   'handle jobA
   'Submit JobB
 Case "JobB"
  'handle JobB
  'Submit JobC
 Case "JobC"
  'Handle JobC
End Select

..same result..the data ocasionaly arrives too late.
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
Why not simply 'blocking' the app with a progressDialog until sync is finished? Seems to me, this is a way to go, if your app relies on this serial process. If however you need to display something else, but just disallow user from doing something until your process is done, you can use a boolean variable which can be checked in your events subs.
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
I use http to my sql server extensively. Review my post regarding adding a unique event for each http request (rather than having to do an If Then, Select Case to narrow down the specific job in one sub)
http://www.b4x.com/forum/basic4android-getting-started-tutorials/18992-httputils2-web-services-now-even-simpler-9.html#post122529

I would then use a progressdialog when you send a job request.

Eg.
B4X:
Sub doJobA
    Dim JobA as httpjob
    JobA.Intialize("JobAName",Me,"JobANameDone")
    JobA.Poststring(server,sql)
    'i've never really had a problem but you may want to have a 
    'timeout timer incase the job never finishes
    ProgressDialogShow2("Loading, Please wait...",False)
End Sub

sub JobANameDone(job as httpjob)
    'process return
    'hide dialog if all done
    ProgressDialogHide
    'turn off timeout timer

    'start a new job
    dim JobB as httpjob
    JobB.Intialize("JobBName",Me,"JobBNameDone")
    JobB.Poststring(server,sql)
    ProgressDialogShow2("Loading, Please wait...",False)
    'restart timeout timer
end sub

I personally use this little sub to handle all my requests.
B4X:
Sub doRemote(JobName,SQL As String) As Boolean
   Dim http As HttpJob
   http.Initialize(JobName,Me,JobName & "Done")
   http.PostString(SQLServer,SQL)
End Sub

I then call it like this:
B4X:
doRemote("JobASql",sql)
'show progess dialog if necessary

And I'd have a sub for each job like this
B4X:
Sub JobASqlDone(job as httpjob)
End Sub

Sub JobBSqlDone(job as httpjob)
End Sub

Good luck!
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Thanks

Thanks, qsrtech

I think you pointed me in the right direction.

Handling everything in the sub JobDone was part of my problem. Actually I was right now looking to a new situation where it would be almost impossible.

Your addition to the httpjob class is precious.

MC73, I do understand your idea, I've tryed something similar. The progress dialog prevents the user from doing anything but that was not my problem. Flags where also not a solution in this very particular case, although I use them a lot in some other situations.

Thank you very much to both of you, I will work on this qsrtech solution to see how it goes.

Jose
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Well

qsrtech, I tryed your solution...but now I can never get to the jobdone..or what ever name I use.

I've changed the httpjob class as in your post:

I added this in Sub Class_Globals
B4X:
Public EventName As String

Then

B4X:
Public Sub Initialize (Name As String, TargetModule As Object, Event As String)
   JobName = Name
   target = TargetModule
   If Event="" Then 
   Event="JobDone"
   End If
   EventName=Event
End Sub

and finaly

B4X:
Public Sub Complete (id As Int)
   taskId = id
   CallSubDelayed2(target, EventName, Me)
End Sub

in the activity or service (I've tryed both) I use

B4X:
       Dim job1 As HttpJob
   job1.Initialize("job1",Me,"")
   job1.PostString(server,sql) ' can't mention here this server name

Then the usual

B4X:
Sub JobDone (job As HttpJob)
   
   If job.Success = True Then
......
.....

The problem now, is that I can see while step debugging I never get to the JobDone sub...I also tryed job1.Initialize("job1",Me,"JobDone"), just to make sure.


If I revert to the standard httpjob...it works...

I'm doing something stupid I guess, but can't see where

Thanks for your help

José
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Oppps

wait...there was a small cap letter in the code...I'll check this tomorrow. Too tired now...puff

Thanks
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
That's good to hear. Sometimes you just need to take a break and come back at it with fresh eyes.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hello, i have the same problem, can you help me? I need the class
Sorry for my bad english
Thank you

This is an old Thread. It´s better to create a new thread for your problem describing what code you have, what happens with your code and what you expect! Did you run into an error?

PS: Vielleicht fängst du erstmal mit einem Thread im Deutschen Forumsteil an. Ist dort vielleicht besser zu erklären wenn Du es nicht so hast mit der englischen Sprache ;)
 
Upvote 0
Top