Android Question Resumable Sub that returns 2 values

Alessandro71

Well-Known Member
Licensed User
Longtime User
How can you write a Resumable Sub that returns 2 values, just like ExecQueryAsync?
I always see Resumable Subs that returns just one value

B4X:
Dim SenderFilter As Object = sql.ExecQueryAsync("SQL", "SELECT * FROM table1", Null)
Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, rs As ResultSet)
 
Solution
B4X:
Sub Button1_Click
  MySub(5,3)
  Wait call_Event (value As Int, name As String)
End Sub

Private Sub MySub(a As Int, b As Int)
  ' operation
  Slepp(100)
  CallSub3(Me,"call_Event",(a+b),"Sum")
End Sub

If you use the "complete" method you can get only one return parameter
B4X:
Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub

Sub Button1_Click
   Dim rs As ResumableSub = Sum(1, 2)
   Wait For(rs) Complete (Result As Int)
   Log("result: " & Result)
   Log("after sum")
End Sub
Maybe return a map

Solutions Development

Member
Licensed User
Longtime User
B4X:
Sub Button1_Click
  MySub(5,3)
  Wait call_Event (value As Int, name As String)
End Sub

Private Sub MySub(a As Int, b As Int)
  ' operation
  Slepp(100)
  CallSub3(Me,"call_Event",(a+b),"Sum")
End Sub

If you use the "complete" method you can get only one return parameter
B4X:
Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub

Sub Button1_Click
   Dim rs As ResumableSub = Sum(1, 2)
   Wait For(rs) Complete (Result As Int)
   Log("result: " & Result)
   Log("after sum")
End Sub
Maybe return a map
 
Last edited:
Upvote 0
Solution

Sagenut

Expert
Licensed User
Longtime User
You could try creating a Type and returning that as the value.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type MyData (Success As Boolean, Something As String)
    Dim MyResult As MyData
End Sub

Private Sub MySub As ResumableSub
    MyResult.Success = True
    MyResult.Something = "Ciao!"
    Return MyResult
End Sub

wait for (MySub) complete (Result As MyData)
Log(Result.Success)
Log(Result.Something)
Of course you will return your ResultSet in one of the value.
In this way you should be able to return as many values as you want.
 
Upvote 1

josejad

Expert
Licensed User
Longtime User
You can return a map.
For example, from this example:

B4X:
Public Sub GetRecord (Command As String, parameters() As String) As ResumableSub
    Dim Answer As Map
    Answer.Initialize
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand(Command, parameters)
    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    Answer.Put("Success", j.Success)
    If j.Success Then
        req.HandleJobAsync(j, "req")
        Wait For (req) req_Result(Res As DBResult)
        'work with result
        req.PrintTable(Res)
        Answer.Put("Message","Data have been read successfully")
    Else
        Log("ERROR: " & j.ErrorMessage)
        Answer.Put("Error", j.ErrorMessage)
    End If
    j.Release
    Answer.Put("Data",Res) '''OJO ESTO DEBERÍA ESTAR DENTRO EL j.Success... COMPROBARLO
    'req.PrintTable(Res)
    Return Answer
End Sub
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I’d rather Dim MyResult inside MySub, to have proper reentrant code
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…