I’m using a slightly modified OkHttpUtils. (It’s modified to be more specialized when communicating with the API on my server.)
In it there's an event _ResponseSuccess that contains this:
B4X:
' Response is an OkHttpResponse
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)
The GetAsynchronously causes an event to the sub below, just as we’re used to:
B4X:
Private Sub Response_StreamFinish (Success As Boolean, theJobId As Int)
DoThings
End Sub
For a couple of different reasons I wanted to update and modernize this to use Wait For instead, like so:
B4X:
' Response is an OkHttpResponse
Wait For (Response.GetAsynchronously("", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)) StreamFinish (Success As Boolean, theJobId As Int)
DoThings
But that didn’t work at all. It crashes with this error message:
B4X:
java.lang.RuntimeException: java.lang.Exception: Sub _streamfinish was not found.
So even though I’m using Wait For, it doesn’t ”catch” the event. Am I doing something wrong here, or what’s going on?
Well, it wasn’t my first choice. I’d rather have used it as is, but I required the http status code, which I couldn’t get otherwise. (I posted a Wish for it a year ago.) And in any case it’s not a rewrite; just a gentle modification.
Thanks for the corrected syntax, I clearly haven’t understood all aspects of resumable subs yet. I’ll revisit your documentation on it.
Since version 2.70 you have access to the status code via the Response object.
V2.70 - New Job.Response field that holds the Response object. It can be used to extract the headers and other information. Note that you need to add a reference to OkHttp / iHTTP in order to use it.
Yeah, I saw something about that, but that boat sailed many moons ago. I'm not going to rip out my battle-tested solution now to replace it with something else, even if it is the official and recommended solution.
I can’t get that to work. No crash or anything, it just never reaches the Wait For.
This is my code, after your syntax correction:
B4X:
Private Sub http_client_ResponseSuccess (Response As OkHttpResponse, theJobId As Int)
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)
Wait For (Response) Response_StreamFinish (Success As Boolean, theJobId As Int)
CompleteJob
End Sub
However, if I remove the Wait For and use a sub instead it works perfectly:
B4X:
Private Sub http_client_ResponseSuccess (Response As OkHttpResponse, theJobId As Int)
Response.GetAsynchronously("response", File.OpenOutput(temp_folder, theJobId, False), True, theJobId)
End Sub
Private Sub Response_StreamFinish (Success As Boolean, theJobId As Int)
CompleteJob
End Sub
From my somewhat naïve understanding of resumable subs your syntax correction looks perfectly reasonable, so I can’t really understand what the problem could be..?
As I’m trying to understand Resumable Subs better, I have to ask: Was it even possible for me to understand this myself, or was this something only you could figure out as you could peek into the inner workings of Response.GetAsynchronously?
First step is to remove the sender filter. It would have then worked.
Next step is to check the value of Sender object. You can see that it doesn't match the value of Response so Response cannot be used as the sender filter parameter.