Currently DBRequestManager's HandleJobAsync code is as follows
A typical use for this method is shown in the example provided in the jRDC2 page ():
The issue I encounter is that if in HandleJobAsync, Success = False, then the Wait For in GetRecord never "finishes".
Modified GetRecord routine:
Called with following loop
will produce the following result when everything goes ok
A solution may be to do a CallSubDelayed2 no matter what. In case of Success = False, return a Null value for the DBResult. This then would need checking in the calling code.
With this change, the logging (upon Success = False) produces
B4X:
Public Sub HandleJobAsync(Job As HttpJob, EventName As String)
Dim ser As B4XSerializator
Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)
ser.ConvertBytesToObjectAsync(data, "ser")
Wait For (ser) ser_BytesToObject (Success As Boolean, NewObject As Object)
If Success = False Then
Log("Error reading response: " & LastException)
Return
End If
Dim res As DBResult = NewObject
res.Tag = Job.Tag
CallSubDelayed2(mTarget, EventName & "_result", res)
End Sub
B4X:
Sub GetRecord (id As Int)
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("select_animal", Array(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)
'work with result
req.PrintTable(res)
Else
Log("ERROR: " & j.ErrorMessage)
End If
j.Release
End Sub
Modified GetRecord routine:
B4X:
Sub GetRecord2 (id As Int)
Log($"GetRecord2 call # ${id}: entry"$)
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("select_studentids", Array())
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
Log($"GetRecord2 call # ${id}: Successful query. Retrieving data"$)
req.HandleJobAsync(j, "req")
Log($"GetRecord2 call # ${id}: Before Wait For"$)
Wait For (req) req_Result(res As DBResult)
Log($"GetRecord2 call # ${id}: After Wait For"$)
'work with result
'req.PrintTable(res)
Else
'Log("ERROR: " & j.ErrorMessage)
Log($"GetRecord2 call # ${id}: ERROR: ${j.ErrorMessage}"$)
End If
j.Release
Log($"GetRecord2 call # ${id}: exit"$)
End Sub
B4X:
For i = 0 To 3
GetRecord2(i)
Next
and the following if Success = False in HandleJobAsyncWaiting for debugger to connect...
Program started.
GetRecord2 call # 0: entry
GetRecord2 call # 1: entry
GetRecord2 call # 0: Successful query. Retrieving data
GetRecord2 call # 0: Before Wait For
GetRecord2 call # 1: Successful query. Retrieving data
GetRecord2 call # 1: Before Wait For
GetRecord2 call # 0: After Wait For
GetRecord2 call # 0: exit
GetRecord2 call # 1: After Wait For
GetRecord2 call # 1: exit
Notice that nothing is executed after the Wait For in GetRecord2?Waiting for debugger to connect...
Program started.
GetRecord2 call # 0: entry
GetRecord2 call # 1: entry
GetRecord2 call # 0: Successful query. Retrieving data
GetRecord2 call # 0: Before Wait For
GetRecord2 call # 1: Successful query. Retrieving data
GetRecord2 call # 1: Before Wait For
Error reading response: (Exception) Not initialized
Error reading response: (Exception) Not initialized
A solution may be to do a CallSubDelayed2 no matter what. In case of Success = False, return a Null value for the DBResult. This then would need checking in the calling code.
B4X:
Public Sub HandleJobAsync(Job As HttpJob, EventName As String)
Dim ser As B4XSerializator
Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)
ser.ConvertBytesToObjectAsync(data, "ser")
Wait For (ser) ser_BytesToObject (Success As Boolean, NewObject As Object)
Dim res As DBResult = Null
If Success = False Then
Log("Error reading response: " & LastException)
Else
res = NewObject
res.Tag = Job.Tag
End If
CallSubDelayed2(mTarget, EventName & "_result", res)
Return
End Sub
Note: This break existing code, so another option may be necessaryWaiting for debugger to connect...
Program started.
GetRecord2 call # 0: entry
GetRecord2 call # 1: entry
GetRecord2 call # 0: Successful query. Retrieving data
GetRecord2 call # 0: Before Wait For
GetRecord2 call # 1: Successful query. Retrieving data
GetRecord2 call # 1: Before Wait For
Error reading response: (Exception) Not initialized
Error reading response: (Exception) Not initialized
GetRecord2 call # 0: After Wait For
GetRecord2 call # 0: exit
GetRecord2 call # 1: After Wait For
GetRecord2 call # 1: exit