B4J Question Sequencing 2 or more CallSubDelayed

Cableguy

Expert
Licensed User
Longtime User
Hi guys...

I'm facing an issue for which I created a temporary workaround, but that risks to be ineffective as time goes by...

I have a button that when clicked takes the content of a TextField and adds it to a MySQL DataBase Table using iRDC2, since this is "variant time consuming" I then use a CallSubDelayed method to call a Refresh sub to Query the updated table, because doing it immediately after the insert query retrieves a not-updated recordset... Also, because of this small time gap, I can't query the table for this last entry's index before the refresh sub finishes... So, and since sequencing CallSubDelayed methods seems to not have the desired effect, I am forced to set a timer to allow a timegap large enough for the refresh to take place...
BUT, I fear that once this table grows large, this approach may also fail...

As always... I explained as best I could, but feel free to ask for clearer explanaitions!

B4X:
.....
timer0.Initialize("setListviewSelectedItem",500)
.....

Sub Button1_Action 'ADD
    Log("button1")
    'INSERT INTO `xpto`.`Country` (`CountryName`) VALUES ('France');
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "insert_Country"
    cmd.Parameters = Array As Object(TextField1.Text)
    reqManager.ExecuteCommand(cmd, "InsertCountry")
'    TextArea1.Text = TextArea1.Text & CRLF & $"INSERT INTO `Country` (`CountryName`) VALUES ('${TextField1.Text}')"$
    tempstring = TextField1.Text
    Log(tempstring)
    TextField1.Text = ""
    CallSubDelayed(Me,"refresh_Listview")
    timer0.Enabled = True
End Sub

Sub setListviewSelectedItem_tick
    timer0.Enabled = False
    ListView1.SelectedIndex = ListView1.Items.IndexOf(tempstring)
    ListView1.ScrollTo(ListView1.Items.IndexOf(tempstring))
    ListView1.RequestFocus
End Sub

Sub refresh_Listview
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "select_Country"
    reqManager.ExecuteQuery(cmd,0, "selectCountry")
'    TextArea1.Text = TextArea1.Text & CRLF & "SELECT CountryName FROM Country"
'    TextArea1.SetSelection(TextArea1.Text.Length, TextArea1.Text.Length)
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
You should call refresh_listview from JobDone (after InsertCountry completed).

that's my problem, I can't figure where in the jobdone to put the refresh call...
my jobdone is in its "original" state as per the jRDC2 manager example

B4X:
Sub JobDone(Job As HttpJob)
   If Job.Success = False Then
     Log("Error: " & Job.ErrorMessage)
   Else
     If Job.JobName = "DBRequest" Then
       reqManager.HandleJobAsync(Job, "ReqManager")
     End If
   End If
   Job.Release
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
following your tip, I got it working!

B4X:
Sub ReqManager_Result(result As DBResult)
    Log("tag: "&result.Tag)
    Select result.Tag
        Case "selectCountry"
            For Each row() As Object In result.Rows
                For Each record As Object In row
                    ListView1.Items.Add(record)
                Next
            Next
            If tempstring <>"" Then
                setListviewSelectedItem
            End If
        Case "InsertCountry"
            refresh_Listview
    End Select
End Sub
 
Upvote 0
Top