B4J Code Snippet [RDC snippet] delegating the response management

Hi Guys...

I'm Using RDC2.
I'n my Main module, I Initialize the DBRequest Class, but instead of "Me", I am passing a Code Module name so that that code module handles all the database related stuff (ResponseManager)
Request.initialize(ResponseManager,"mydatabaselink")

So, making a BDRequest like Main.RequestManager.blablabla seems to be executed BUT always returns a unsuccessful http job.

So I decided to make it as "usual" and used "Me" and copied the necessary unchanged subs from the response manager to main and it works as expected...

Should I assume that my initial approach is simply impossible? or should I initialize the RequestManager inside the ResponseManager module?

Thanks in advance
 

Harris

Expert
Licensed User
Longtime User
Good question. I have fought with this myself (over and over years ago).

It is where the JobDone sub resides... Tricky, but doable.
I think, but don't quote me, a JobDone needs to reside in the calling code mod. Specific to this mod.

I have them sprinkled throughout my code in B4A, yet cannot recall them right now.

Request.initialize(ResponseManager,"mydatabaselink")

You may need a JobDone in 'mydatabaselink' mod.

Everything else about RDC is quite simple and logical. This issue can trip you up... as it did me.
 

Cableguy

Expert
Licensed User
Longtime User
I ended up by using a mix classic approach, using a bare one job done sub and rdbrquest(response) sub and then passing the necessary info to the response manager subs using callsub....
This way I can keep my main clean and know exactly where to tweak things without fear of breaking my code
 

Harris

Expert
Licensed User
Longtime User
I ended up by using a mix classic approach, using a bare one job done sub and rdbrquest(response) sub and then passing the necessary info to the response manager subs using callsub....
This way I can keep my main clean and know exactly where to tweak things without fear of breaking my code
Nice...

A short tutorial may help me clean up my code (mess) to take advantage of this.

Thanks
 

Cableguy

Expert
Licensed User
Longtime User
I will, but there's no magic there... Later today I'll post it
 

Cableguy

Expert
Licensed User
Longtime User
So, Here is how I did it:

In Main module I declared the needed types and the reference to the DBRequestManager

B4X:
Public RequestManager As DBRequestManager
Type DBResult (Tag As Object, Columns As Map, Rows As List)
Type DBCommand (Name As String, Parameters() As Object)

Notice the "Public" declaration for the RequestManager. This way I can make calls to it using Main.RequestManager from whichever module I use

Then comes the needed Initialization, a standart one

B4X:
RequestManager.Initialize(Me, "http://www.mywebsite.com:17178/rdc")

then the subs...
This one is well commented, I think, so no further explanation...
B4X:
Sub JobDone(Job As HttpJob)
CallSub2(ResponseManager, "DBTransactionDone", Job) 'ResponseManager is a static code module, DBTransactionDone is basically the same as JobDone
'This Block NEEDS to be here so that the responses can be trapped by ReqManager sub
If Job.Success = True Then
If Job.JobName = "DBRequest" Then
RequestManager.HandleJobAsync(Job, "ReqManager")
End If
End If
Job.Release
End Sub

Here we will, as usual, retrieve the request responses...
I am yet to decide, but I am thinking on create a select case and use CallSubDelayed to trigger individual subs in the ResponseManager code module
B4X:
Sub ReqManager_Result(result As DBResult)
Log("SQLCommand tag: "&result.Tag)
End Sub

As I said, no magic... just "clever" (??) delegating

[[EDIT]]

This sub is an example on how I can call the RequestManager.
Notice how the Main declared types CAN be used outside the Main module without re-declaring them in each module
B4X:
Sub TryToConnect_Tick
AntaresUtils.SetShadow(DataBaseConnectionStatusLed,"InnerShadow", fx.Colors.Yellow,1,1,15)
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = "TestConnection"
Main.RequestManager.ExecuteQuery(cmd,0,"TestConnection")
End Sub
 
Last edited:
Top