Android Question Best Pratice

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi all,

I have a doubt about what could be the best method to asynchronously receive web response from HTTP request... Imagine a service that does requests until receive something like "EOF" from server... we could adopt two programming approaches in a service:

1. Call the Job.Download or Job.Post service in Service_Create routine with the callback response pointed to the same service. ( Job1.Initialize("Name",Me))

In this approach, in JobDone event it's needed to call the Download, Post... again until get EOF from server... the first call must to be in Create instead of in the start session of the service because in Start could generate an "infinite loop" because each callback of job would "start" again the process, independent of the "EOF"...
There is a side effect of this approach also... To start again the download cycle the service must to be stopped after receiving EOF from server... this could allow an external call startservice to restart the check for new data (because if the service is already running, create doesn't happens with startservice)

OR

2. Point the call back routine to another service... ( Job.Initialize("Name",Service2) ) .. Something like this:

Service1:

B4X:
Service_Start
  ...
     Job.Initialize("Name",Service2)
    ...
     Job.PostString(Link)
...

Service2:


B4X:
Sub JobDone(Job as hhtpjob)

    If Job.Success then
         If Job.GetString <> "EOF" then
               Blah  Blah
               StartService(service1)
        end if
    end if


Of course that for each approach I have some attention points:

For 1:
- I must to stop the service in "EOF" to be sure of being able to restart the http requests process when needed

For 2:
- I have two services instead of only one... the second service only to take care of the callback calls from httpjobs... (this solution I already used when needing to call a HttpJob in an activity, in order to avoid the activity reopen after some delayed callbacks from web...)

In your opinion community, what's the best approach ? I think that it's a common problem for all of us when using httpjobs...
 

Roycefer

Well-Known Member
Licensed User
Longtime User
I think you should use Wait For:
B4X:
Dim again As Boolean = True
Do while again
    Dim h As HttpJob
    h.Initialize("",Me)
    h.Download(....)
    Wait For (h) JobDone(Job As HttpJob)
    If Job.GetString<>"EOF" Then
        'Do some stuff
    Else
        'Do some other stuff
        again = False
    End If
    Job.Release
Loop
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User

Hi!

Great approach also! But anyway, this code must to be in "Service_Create" no ? ( because if in Service_Start could generate an "infinite loop" even after "EOF" right ?) . Then, in EOF we must to add stopservice(ME) to be able to start the cycle again when needed...
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You can try this, put that code in a Sub in your Service:
B4X:
Sub RunDownloadLoop
   Dim again As Boolean = True
   Do while again
       Dim h As HttpJob
       h.Initialize("",Me)
       h.Download(....)
       Wait For (h) JobDone(Job As HttpJob)
       If Job.GetString<>"EOF" Then
           'Do some stuff
       Else
           'Do some other stuff
           again = False
       End If
       Job.Release
   Loop
End Sub

And then you can call
B4X:
CallSubDelayed(Me, "RunDownloadLoop")
whenever you need to. You can have two or more loops running simultaneously, if you manage your global variables correctly. And you can start new loops after old ones have ended. You can call this from Service_Create, from Service_Start or anywhere else.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User

Hi!

StopService and put in Service_Create is not a bad choice... and also frees memory in device when the service is not needed... I think that the main question is: is there any issue having the callback from httpjob in a separated service?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Apart from code-spaghetti issues, I don't think there should be any serious problem with having the callback Sub be in another Service. However, you should test it before you deploy that strategy.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Apart from code-spaghetti issues, I don't think there should be any serious problem with having the callback Sub be in another Service. However, you should test it before you deploy that strategy.
Hello,

I tested and worked fine... like you told, it looks to be a more "clean" solution, besides the code-spaghetti.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I guess code really is more like art than it is like science. For me, the solution with Wait For seems much more clean. But you should use whichever code that both works and makes the most sense to you.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I guess code really is more like art than it is like science. For me, the solution with Wait For seems much more clean. But you should use whichever code that both works and makes the most sense to you.
Hello my friend... yes, we are artists - and like any art, sometimes a creation is horrible for some and fantastic for others . I think that is it.

Thanks for answering!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…