Android Question Return value from a sub with a call to Wait For

nirmelamoud

Member
Hi,

I have a sub that use Wait For in order to call rest interface, I want to make it a sync call and return value, can I do that ?
or do I have to use call chain at the end and call other function with the 'return' value
 

nirmelamoud

Member
Thanks, but not what I was asking,

Im asking if there is a way to do this
funca (1)
...
ret = funcb()
... do something with ret
...

funcb()
....
http call in a sync way (wait for....)
....
return job.getText

like in Java for example, it sound like B4A automatically run funcb in a separate thread so you cant get value back in funca
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
like in Java for example
There is no such feature in Java. Resumable subs are similar (but not exactly the same) to C# async / await feature.
it sound like B4A automatically run funcb in a separate thread so you cant get value back in funca
No additional threads are created for resumable subs.

The way to implement the logic that you are describing is with subs that return ResuableSub + Wait For.

B4X:
Sub FuncA
 Wait For (FuncB) Complete (Result As Something)
 Log(Result)
End Sub

Sub FuncB As ResumableSub
 Dim j As HttpJob
 ...
 Dim res As String = j.GetString
 j.Release
 Return res
End Sub
 
Upvote 0

nirmelamoud

Member
of course I can do that in Java, but regardless, what you are saying is that I can't do that in basic and I need to use callback-like functionality or with double wait for - ok
thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
of course I can do that in Java,
No, you can't. There is no equivalent feature in Java.

Even trivial code such as this will be much more complicated in Java:
B4X:
For i = 1 To 10
 btn.SetVisibleAnimated(100 * i, i Mod 2 = 0)
 Sleep(100 * i)
Next
And of course that you can't hold the main thread or touch the UI from a background thread.
 
Upvote 0

nirmelamoud

Member
of course, not meant to hold the UI thread,

Java 11 HttpClient​

More than twenty years after HttpURLConnection we had Black Panther in the cinemas and a new HTTP client added to Java 11: java.net.http.HttpClient. This has a much more logical API and can handle HTTP/2, and Websockets. It also has the op
 
Upvote 0

nirmelamoud

Member
I agree resumable is a nice feature , when I was talking about the java feature is to implement what I was asking about which is sync call to HTTP get
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
or with double wait for
Even a double Wait For does not block. That's the beauty of Wait For, you don't have to worry about grinding the event queue to a halt. Looking at the doc for the Java 11 implementation, both HttpClient.send and CompletableFuture.get are fully blocking, therefore forcing you to use threads in cases where the return may take a while (since at that point, the event queue would be blocked). Now, if you are looking at a full blocking HTTP request that stalls out the event queue, then you'll have to come up with something else.
 
Upvote 0
Top