Android Question Async vs Not

MrKim

Well-Known Member
Licensed User
Longtime User
As a died in the wool VBA programmerI am new to the world of Asynchronous getting of data - and I hate it!

At the moment I am being forced to use it because that is all that works. But it means this:

B4X:
Sub Button1_Click
    'Do not forget to load the layout file created with the visual designer. For example:
    Try
        sql1.InitializeAsync("sql1", "net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://192.788.1.55:99999;databaseName=mydb;user=myuser;password=mypw", "myuser",  "mypw")  'sql 2014
        Log("Connection Successful ")
        'sql1.Close
    Catch
        Log("connection error ")
        Log(LastException.Message)
        Return
    End Try
    Dim C As ResultSet
    C = sql1.ExecQuery("Select TOP 10 Jb_JOb_Num, Jb_cust_Code FROM Jobs ORDER BY Jb_JOb_Num desc;")
    Do While C.NextRow
        Log(C.GetString("Jb_JOb_Num") & ": " & C.GetString("Jb_cust_Code"))
    Loop
    '    sql1.ExecNonQuery("Insert INTO JOBS (Jb_Job_Num, Jb_Cust_Code, Jb_Part_Num, Jb_Part_Rev) SELECT 'QQQQQQ', 'dsfasf', 'wertwert', '45'")
    C = sql1.ExecQuery("Select TOP 10 Jb_Job_Num, Jb_Cust_Code, Jb_Part_Num, Jb_Part_Rev FROM Jobs WHERE Jb_Job_Num = 'QQQQQ';")
    Do While C.NextRow
        Log(C.GetString("Jb_JOb_Num") & ": " & C.GetString("Jb_cust_Code"))
    Loop
End Sub

Has Become THIS!:
B4X:
Sub Button1_Click
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Try
        sql1.InitializeAsync("sql1", "net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://192.788.1.55:99999;databaseName=mydb;user=mypw;password=mypw", "mypw",  "mypw")  'sql 2014
        Log("Connection Successful ")
        'sql1.Close
    Catch
        Log("connection error ")
        Log(LastException.Message)
        Return
    End Try
End Sub

Sub SQL_QueryComplete (Success As Boolean, Crsr As ResultSet)
    If Success Then
        Do While Crsr.NextRow
            Log(Crsr.GetString("Jb_JOb_Num") & ": " & Crsr.GetString("Jb_cust_Code"))
        Loop
    Else
        Log(LastException)
    End If
End Sub

Sub sql1_Ready (Success As Boolean)
    Log(Success)
    If Success = False Then
        Log(LastException)
        Return
    End If
    Log(1)
    'Dim C As ResultSet
    Log(2)
    Dim L As List
    L.Initialize
    sql1.ExecQueryAsync("SQL", "Select TOP 10 Jb_JOb_Num, Jb_cust_Code FROM Jobs ORDER BY Jb_JOb_Num desc;", L)
    Log(3)
    Log(4)
    sql1.AddNonQueryToBatch("Insert INTO JOBS (Jb_Job_Num, Jb_Cust_Code, Jb_Part_Num, Jb_Part_Rev) SELECT 'ZXZXZXZXXX', 'dsfasf', 'wertwert', '45'", Null)
    sql1.ExecNonQueryBatch("Batch")
    Log(5)
End Sub


Sub Batch_NonQueryComplete (Success As Boolean)
    Log("NonQuery: " & Success)
    If Success = False Then
        Log(LastException)
    Else
        sql1.ExecQueryAsync("Jobs", "Select TOP 10 Jb_Job_Num, Jb_Cust_Code, Jb_Part_Num, Jb_Part_Rev FROM Jobs WHERE Jb_Job_Num = 'ZXZXZXZXXX';", Null)
    End If
End Sub

Sub Jobs_QueryComplete (Success As Boolean, Crsr As ResultSet)
    Do While Crsr.NextRow
        Log(Crsr.GetString("Jb_JOb_Num") & ": " & Crsr.GetString("Jb_cust_Code"))
    Loop

End Sub

Which is some of the snakeiest hard to follow code I have ever written.
Does anyone have any suggestions or tips on how to write readable code when you have to do a lot of async? I mean I have to execute a LOT of queries sequentially and they are usually in one routine. Now I am looking at jumping from one routine to the next. It's awful.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Thank you, Yes! Resumable subs does solve a lot of the problem. The only issue missing there is that Return doesn't seem to work so in some ways I still can't compartmentalize as much as I would like. But it is a HUGE step forward.

I thank the powers that be at Anywhere Software for your amazing work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The only issue missing there is that Return doesn't seem to work so in some ways I still can't compartmentalize as much as I would like.
Currently resumable subs cannot return values at all, you will get a compiler error.

I do plan to add support for returning values while the calling sub waits for the results.
 
Upvote 0
Top