B4J Question How to handle Jrdc2 in multi form application

MarcRB

Active Member
Licensed User
Longtime User
Hi, I am experimenting with B4J and specific with Jrdc2.
I followed the great example and it works nice, but I can only access the database in main module.

Because I will use it in a multi form application, where each form has it's own code-module I was looking for a way to move the JRDC specific code to an other place that can be accessed from anywhere in the application.
I moved the next code from main to a new code module named modJrdc2
B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
    Type DBCommand (Name As String, Parameters() As Object)
    Private const rdcLink As String = "http://192.168.2.34:17178/rdc"
End Sub

Public Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, rdcLink)
    Return req
End Sub

Public 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

In each form where I want to execute select or inserts I placed simular code as below.
Note that functions are pointed to modJrdc2.
B4X:
Sub Load_UserNames
    Dim req As DBRequestManager = modJrdc2.CreateRequest
    Dim cmd As DBCommand = modJrdc2.CreateCommand("select_usernames", Null)
    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)
        cbGebruikers.Items.Clear
        For Each row() As Object In res.Rows
            cbGebruikers.Items.add(row(0))
        Next
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub

There is no error at all. But the spinner didin't fill any data.
Before with all the code in main it works very well.
 

OliverA

Expert
Licensed User
Longtime User
The problem is that the when you call modJrdc2.CreateRequest, it creates a DBRequestManager object that is linked to the static module
B4X:
    req.Initialize(Me, rdcLink)
That Me is the static module's Me. Now hen you call req.HandleJobAsync, that method ends in
B4X:
    CallSubDelayed2(mTarget, EventName & "_result", res)
where mTarget is set to the Me of the static module and therefore this CallSubDelayed2 is trying to call the req_result method in the static module, not the module that contains your Load_UserNames sub. I think I may have done something like this that you can either use or maybe use as a starting point for your own static module of jRDC functionality. The module I'm talking about is call jRDC2Utils and it is part of my B4J client that tests my modded jRDC2 server

B4J Client: https://www.b4x.com/android/forum/threads/jrdc2-client-example-using-modded-jrdc2.85581/
Modded jRDC2 server: https://www.b4x.com/android/forum/threads/modded-jrdc2-w-sqlite-support-and-more.85578/#post-542206
 
Upvote 0
Top