Android Question Hold sub until JobDone

SamuelP

Member
Licensed User
When the user clicks at a list entry, I want to open a new activity with details to the entry. The data is coming from sql-server via RDC.

I found no working way to have all the code, that is needed to get the DBResult-object, in a seperat module.
My goal was to be able to have something simple like:
B4X:
Sub ShowDetails(KL_ID As Int)
   
    Dim res As DBResult = dbHelper.GetResult("select_Klient",KL_ID")
   
    Dim dt As DBResult = res
    Dim dr() As Object = res.Rows.Get(0)
   
    txt_Nachname.Text = dr(dt.Columns.Get("Nachname"))
    txt_Vorname.Text = dr(dt.Columns.Get("Vorname"))
   
End Sub

But I found no way to let the sub wait, till the DBResult is read to use.

The only working solution is to have all this code in each sub:

B4X:
    Dim req As DBRequestManager = CreateRequest(Me)
    Dim cmd As DBCommand = CreateCommand("select_Klient", Array(KL_ID))
  
    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        req.HandleJobAsync(j, "req")
        Wait For (req) req_Result(Res As DBResult)
    Else
        Log("ERROR: " & j.ErrorMessage)
        Return
    End If
    j.Release

It also seems that I have to have this in each activity, cause it was not working, when I had it in a own module.
B4X:
Sub CreateRequest(Target As Object) As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Target, "http://80.121.139.239:17178/rdc")
    Return req
End Sub

Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub



So, is there any way to have less code, when retrieve data from RDC in several subs/activities?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Create a class module named DBHelperClass.
2. Declare a public process global named DBhelper in the Starter service and initialize it in Service_Create.
3. Implement GetResult:
B4X:
Public Sub GetResult(Target As Object, Other parameters here)
send the request
wait for the result
CallSubDelayed2(Target, "DBHelper_Result", Result)
End Sub
4. Use it like this:
B4X:
Starter.DBHelper.GetResult(Me, ...)
Wait For DBHelper_Result (res As DBResult)
'...
 
Upvote 0

JNG

Member
Licensed User
Hi Samuel

I am also looking for same kind of solution can you pl share the process how you implemented ( small example)
Thanks in advance

regards
jng
 
Upvote 0
Top