I had this all working, but when re-factoring and doing a slight bit of abstraction I started to confuse myself beyond belief. Now I feel I have reached a point where I understand nothing on how Resumable Subs work. I appreciate if someone could explain where I'm off.
I have a class (CLNET) defined in my Starter that handles my network requests, and at it's core, there's a generic sub that does the HttpJob handling.
For my situation, imagine a sub in an Activity calling a sub in my net class, which is then calling another sub in the same class. The last sub does the HttpJob and waits for it to finish, and the result of that should pour all the way back to the sub in the activity, which is waiting for the result.
This my structure (simplified):
In the Main activity
In CLNET
I've tried lots of variations of this. At one point it kind of felt logical that only the genericRequest should do any Wait For'ing, because the btnExample_Click and ExampleJob would just wait for it to finish. Like so:
In the Main activity
In CLNET
But that didn't work as expected. Like I said, I'm getting more and more confused. I'm sure there's something obvious I'm missing on how to think of the Resumable Subs and code flow. All help appreciated, preferably attached to an explanation.
I have a class (CLNET) defined in my Starter that handles my network requests, and at it's core, there's a generic sub that does the HttpJob handling.
For my situation, imagine a sub in an Activity calling a sub in my net class, which is then calling another sub in the same class. The last sub does the HttpJob and waits for it to finish, and the result of that should pour all the way back to the sub in the activity, which is waiting for the result.
This my structure (simplified):
In the Main activity
B4X:
Sub btnExample_Click
Wait For (Starter.CLNET.ExampleJob(Starter.SomeValue)) complete (result As Map)
doSomething(result)
End Sub
In CLNET
B4X:
Sub ExampleJob (SomeValue as string) As ResumableSub
Dim url as string = "http://www.example.com/" & SomeValue
Wait For (genericRequest(url)) complete (result As Map)
return result
End Sub
Private Sub genericRequest (url as string) As ResumableSub
Dim job as HttpJob
job.Initialize("", Me)
job.Download(url)
Wait For (job) JobDone(job As HttpJob)
Dim result as Map = jobToMap(job)
Return result
End Sub
I've tried lots of variations of this. At one point it kind of felt logical that only the genericRequest should do any Wait For'ing, because the btnExample_Click and ExampleJob would just wait for it to finish. Like so:
In the Main activity
B4X:
Sub btnExample_Click
Dim result As Map = Starter.CLNET.ExampleJob(Starter.SomeValue)
doSomething(result)
End Sub
In CLNET
B4X:
Sub ExampleJob (SomeValue as string) As Map
Dim url as string = "http://www.example.com/" & SomeValue
return genericRequest(url)
End Sub
Private Sub genericRequest (url as string) As ResumableSub
Dim job as HttpJob
job.Initialize("", Me)
job.Download(url)
Wait For (job) JobDone(job As HttpJob)
Dim result as Map = jobToMap(job)
Return result
End Sub
But that didn't work as expected. Like I said, I'm getting more and more confused. I'm sure there's something obvious I'm missing on how to think of the Resumable Subs and code flow. All help appreciated, preferably attached to an explanation.
Last edited: