Android Question Looping queries to JRDC

proinko

Member
Licensed User
Longtime User
Hi all

i am trying to send a loop of queries to the server (using JRDC2) but it seems that each query replaces the previous one.

If i run the code in debug mode (pausing on each line) it executes ok, so i think that i let the answers comes back from the server before sending the next query

This is the code

1) the loop that send the queries

B4X:
    For i=0 To lst.Size-1
        wait For (CheckT(lst.Get(i))) complete (mresult As Map)
        If mresult.Get("ReqOK")=True Then
            ....
            Return
        End If
    Next

2) the sub that send each query

B4X:
Sub CheckT(P As String)As ResumableSub
    Dim m As Map
    m.Initialize
    m.Put("ConnOk",False)
    m.Put("ReqOK", False)

    Dim Req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand("QueryName",Array(P))
    Wait For (Req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        m.Put("ConnOk",True)
        Req.HandleJobAsync(j, "req")
        Wait For (Req) req_Result(res As DBResult)
        'Req.PrintTable(res)
        If res.Rows.Size>0 Then
            Dim record() As Object=res.Rows.Get(0)
            If record(res.Columns.Get("cTAG"))=0 Then
                     m.Put("ReqOK", True)
            End If
        End If
    Else
        Log("ERROR: " & j.ErrorMessage)
        Msgbox(j.ErrorMessage,"Error")
    End If
    j.Release
    Return m
End Sub


I guess that i am missing something about resumable subs ...

thanks!
 

OliverA

Expert
Licensed User
Longtime User
i am trying to send a loop of queries to the server (using JRDC2) but it seems that each query replaces the previous one.
What do you mean by this?
B4X:
For i=0 To lst.Size-1
        wait For (CheckT(lst.Get(i))) complete (mresult As Map)
        If mresult.Get("ReqOK")=True Then
            ....
            Return
        End If
    Next
As this code stands:
1) It will complete one Wait For before executing the next.
2) The first mresult.Get("ReqOK") that returns True, ends your For loop (via Return), even if more items are in the list. I don't know if that is the effect you are desiring. Also, the Wait does a Return to the code after the call to whatever sub that contains the For loop is executed (unless you have another Wait For there).
 
Upvote 0

proinko

Member
Licensed User
Longtime User
What do you see?

Add Log("Sending request") before the call to CheckT. Are the messages being sent sequentially?
What do you see?

Add Log("Sending request") before the call to CheckT. Are the messages being sent sequentially?

yes, adding Log("Sending request" & i) returns Sending Request0, Sending Request1, ... sequentially, until the last ítem of the list (there is no one left)
 
Upvote 0

proinko

Member
Licensed User
Longtime User
What do you mean by this?
B4X:
For i=0 To lst.Size-1
        wait For (CheckT(lst.Get(i))) complete (mresult As Map)
        If mresult.Get("ReqOK")=True Then
            ....
            Return
        End If
    Next
As this code stands:
1) It will complete one Wait For before executing the next.
2) The first mresult.Get("ReqOK") that returns True, ends your For loop (via Return), even if more items are in the list. I don't know if that is the effect you are desiring. Also, the Wait does a Return to the code after the call to whatever sub that contains the For loop is executed (unless you have another Wait For there).

thanks for your replies

... sorry, trying to clean the code to post it, i left that "return" on the wrong place...

What i am trying to do is to check if every ítem on the list has it corresponding record on the database (all of them exists int the examples i am checking)

The code works sometimes (it returns that all of them exist), but , with the same queries, sometimes fails and returns that no one record exists. This is why i thought that maybe some of the queries are overwritting others, depending if the server is busy and the reply takes some time ...

So, forgetting that "return" (sorry again), do you think that the code is right, and that all the queries should execute?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
all the queries should execute
They should. One thing to look out for is not to modify your list (lst) object while processing it, or you will experience unpredictable results.
 
Upvote 0

proinko

Member
Licensed User
Longtime User
Ok, thanks for your time Oliver and Erel

Knowing that the code corresponding to the queries loop is not wrong, i will check on the rest of the code (the list items are not modified during the loop)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
i will check on the rest of the code

You may already be doing this, but you could run you jRDC server from the command line. Then launch your application and let it process the loop. You should be seeing corresponding log outputs to the console window by jRDC. Example output:
Command: query: TheQueryNameIwasExecuting, took: 5ms, client=192.168.1.10
 
Upvote 0

proinko

Member
Licensed User
Longtime User
I am watching at the jRDC server now, but in the last n+1 executions, all the queries are working ok ...

I have added just one line in the code, instead of

B4X:
  If record(res.Columns.Get("cTAG"))=0 Then
                     m.Put("ReqOK", True)
            End If

the code is now

B4X:
 Dim N as int=record(res.Columns.Get("cTAG"))   
            If N=0 Then
                     m.Put("ReqOK", True)
            End If

the queries do not fail now (so far), but i do not think that this is the solution to be sure in the next queries

Thanks for your help
 
Upvote 0
Top