Android Question Coding RDC Client to work Asyncronously?

Enrico Fuoti

Active Member
Licensed User
Longtime User
Hello all,
I created a small app that retrieves and writes datas to a remote mysql server.
Everything works fine, but the communication with the server stops the application flow until the data retrieval is completed..
It looks like the application holds at the "Jobdone" sub. The nature of my app requires a continuous flow...
Creating a small separate app with a service that periodically syncronize the remote database with the local one may be a solution, but I understand that there may be a way to run the rdc calls asyncronously.. Any suggestion?
 

Enrico Fuoti

Active Member
Licensed User
Longtime User
Thank you Erel,
that is what I thought it would happen. I may be missing something.
Actually I have a timer that fires every second checking an astream bluetooth connection to be on. When I receive the data it stops until the transfer is finished. Also when transfer is on anything seems to be freezed. :-/
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
are you sure your app hangs before JobDone and not in the following data elaboration ?
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Yes,
this is the Jobdone event..
B4X:
'---------------------------
Sub JobDone(Job As HttpJob)
'---------------------------
' Variabili di appoggio -------------------------------------
   Dim ls_cognome, ls_nome, ls_badge, ls_codice_badge, ls_cod_fiscale As String
   Dim lf_saldo_credito, lf_costo1, lf_costo2, lf_costo3, lf_costo4 As Float
   Dim ls_costo1, ls_costo2, ls_costo3, ls_costo4 As String
   Dim li_litri As Int
   Dim li_bonus1, li_bonus2, li_bonus3, li_bonus4 As Int
   Dim ls_syncdate As String
   Dim ld_syncdate As Long
   Dim li_rows As Int
   Dim ls_rows As String
   Dim ls_free1, ls_free2, ls_free3, ls_free4 As String
   DateTime.DateFormat = "dd-MM-yyyy HH:mm"
   Dim ls_tag As String
  If Job.Success = False Then
  Log("Error: " & Job.ErrorMessage)  
    ToastMessageShow("Errore di sincronizzazione DB su server" & gs_server &"!", False)
  Else
  If Job.JobName = "DBRequest" Then
  Dim result As DBResult = reqManager.HandleJob(Job)
    'ls_tag = result.Tag
    Select Case ls_tag
       Case "Borsellino"  'query tag
        li_rows = result.Rows.Size
         If li_rows > 0 Then
           ' svuota db locale   
           s.ExecnonQuery("delete from utenti")   
           ls_rows = NumberFormat(li_rows, 0, 0)  
           Label_lv_server.text = "N.ro records Utenti su DB Server:" &  ls_rows
           '- Sync minisql locale -------------------------  
           lv_letture_server.Clear    
        For Each records() As Object In result.Row
               ls_cod_fiscale = records(result.Columns.Get("cod_fiscale"))
               ls_cognome = records(result.Columns.Get("cognome"))
               ls_nome = records(result.Columns.Get("nome"))
               ls_badge = records(result.Columns.Get("badge"))
ls_syncdate  = records(result.Columns.Get("syncdate"))     
               DateTime.DateFormat= "yyyy-MM-dd HH:mm:ss"
               ' convert to date time
               ' my sql ritorna solo strsaldo_bonusinghe converte in date time       
               ld_syncdate = DateTime.DateParse(ls_syncdate)
               ' ... other columns.....
                          
               'Copy to local DB ---------------------      
               s.ExecNonQuery2("Insert into utenti (rowid, cod_fiscale, cognome, nome, badge) Values ( null,?,?,?,?)",Array As Object(ls_cod_fiscale, ls_cognome, ls_nome, ls_badge, ld_syncdate))  
               DateTime.DateFormat= "dd-MM-yyyy  HH:mm"
               ls_syncdate = DateTime.date(ld_syncdate)    
               lv_letture_server.AddTwoLines(ls_codice_badge & " - " & ls_cognome & " - " & ls_nome, _
                        "Credito:" & NumberFormat(lf_saldo_credito, 0, 2) & " Bonus:" & li_bonus1 & " - " & li_bonus2 & " - " & li_bonus3 & " - " & li_bonus4 & "  Costo: " & NumberFormat(lf_costo1, 0, 2)   & " - "  & NumberFormat(lf_costo2, 0, 2) & " - "  & NumberFormat(lf_costo3, 0, 2) & " - "  & NumberFormat(lf_costo4, 0, 2)    & " - Sync:"  & ls_syncdate )
               'Log(name)
           Next
               'Timercheckserverlist.Initialize("Timercheckserverlist", 360000)
             leggi_borsellino_android
             Else ' No rows
               ToastMessageShow("Nessun record trovato",False)
               'Return  
             End If
         Case "Erogazioni"
           li_rows = result.Rows.Size
           ls_rows = NumberFormat(li_rows, 0, 0)
           s.ExecnonQuery("delete from erogazioni_utenti ;")      
           ToastMessageShow("DB erogazioni Android sincronizzato su server: " & gs_server & Chr(10) & "Trasferite: " & ls_rows  & " erogazioni!", False)'  
           lv_erogazioni.Clear
           Label_lv_erogazioni.text = "N.ro records erogazioni non trasferite:" &  ls_rows
           Timersyncerogretry.enabled = False
         Case "Errori"
           li_rows = result.Rows.Size
           ls_rows = NumberFormat(li_rows, 0, 0)
           s.ExecnonQuery("delete from errors_log ;")      
           ToastMessageShow("DB log errori Android sincronizzato su server: " & gs_server  & Chr(10) & "Trasferiti: " & li_rows  & " errori!", False)'  
           leggi_errori_android  
        End Select
        End If  'JOB
   End If   'DB Request
  Job.Release
End Sub
 
Last edited:
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Yes,
This is the Jobdone event a bit shortened for readability,..

B4X:
'---------------------------
Sub JobDone(Job As HttpJob)
'---------------------------
' Variabili di appoggio -------------------------------------
   Dim ls_cognome, ls_nome, ls_badge, ls_codice_badge, ls_cod_fiscale As String
   Dim ls_syncdate As String
   Dim ld_syncdate As Long
   Dim li_rows As Int
   Dim ls_rows As String
   DateTime.DateFormat = "dd-MM-yyyy HH:mm"
   Dim ls_tag As String
  If Job.Success = False Then
     Log("Error: " & Job.ErrorMessage)  
     ToastMessageShow("Errore di sincronizzazione DB su server" & gs_server &"!", False)
  Else
    If Job.JobName = "DBRequest" Then
     Dim result As DBResult = reqManager.HandleJob(Job) '------->>> it looks like the hold happens  here..
     ls_tag = result.Tag
     Select Case ls_tag
       Case "Borsellino"  'query tag
          li_rows = result.Rows.Size
          If li_rows > 0 Then
           ' svuota db locale   
           s.ExecnonQuery("delete from utenti")   
           ls_rows = NumberFormat(li_rows, 0, 0)  
           Label_lv_server.text = "N.ro records Utenti su DB Server:" &  ls_rows
           '- Sync minisql locale -------------------------  
           lv_letture_server.Clear    
        For Each records() As Object In result.Row
               ls_cod_fiscale = records(result.Columns.Get("cod_fiscale"))
               ls_cognome = records(result.Columns.Get("cognome"))
               ls_nome = records(result.Columns.Get("nome"))
               ls_badge = records(result.Columns.Get("badge"))
                ls_syncdate  = records(result.Columns.Get("syncdate"))     
               DateTime.DateFormat= "yyyy-MM-dd HH:mm:ss"
               ' convert to date time
              ld_syncdate = DateTime.DateParse(ls_syncdate)

               .....more fields .....
              
               'Copy record to local db---------------------      
               s.ExecNonQuery2("Insert into utenti (rowid, cod_fiscale, cognome, nome, badge, syncdate) Values ( null,?,?,?,?,?)",Array As Object(ls_cod_fiscale, ls_cognome, ls_nome, ls_badge, ld_syncdate))  
               DateTime.DateFormat= "dd-MM-yyyy  HH:mm"
               ls_syncdate = DateTime.date(ld_syncdate)    
               lv_letture_server.AddTwoLines(ls_codice_badge & " - " & ls_cognome & " - " & ls_nome, _
                        " - Sync:"  & ls_syncdate )
           Next
             leggi_borsellino_android  'reads from minisql DB
           Else ' No rows
               ToastMessageShow("Nessun record trovato",False)
               'Return  
           End If
    
        End Select
        End If  'JOB
   End If   'DB Request
  Job.Release
End Sub

Enrico
 
Last edited:
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Erel,
sorry forgot tags, this is my first post..
I'm not sure to understand what you mean when you suggest to use a single transaction.
The rdc server command is a single mysql select ...
How else could i process the result?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I suspect your problem starts here: "For Each records() As Object In result.Row"
Try to log some datetime in various point and you will see that loop is the part of code that takes lot of time to complete.
Try to use a single transaction as Erel says, the tutorial is very clear.
You will speed up things, but if you still have "hangs" problems you can consider to make use of the "threading library".
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Thank you Marcick,
i didn't know the threading library , I'll give it a try.
Erel suggestion to use transactions is very useful to speed up local inserts, but I still face the hanging problem.
As you suggested i did some tests using debug and retrieving a larger number of rows from mysql server.
I was able to verify that the hangs happens at this line:

Dim result As DBResult = reqManager.HandleJob(Job)

It holds there for 12/15 seconds while retrieving 5000 records over wifi connection.
Tablet meanwhile is totally frozen. Even the waiting circle stops turning..

Enrico
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I can't find now the thread where I had the same problem of you, but the point is that when you are in JobDone all the network stuff has been completed.
You need now the threading library to put that HandleJob and the other for-next loop in a separate process without freezing the main one.
I remember there are some issue in debug mode with that library but was the only solution.
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Erel,
Yes the problem seems to be within the loop in the "reqManager.HandleJob" sub thath stops everything until completed.
I'm not sure on how i could transfer this process execution in a timer event...
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Well,
I think I'll try to test both options and figure out what performs better for my app..
Thank you all for helping..
Enrico
 
Upvote 0
Top