Android Question Coding RDC Client to work Asyncronously?

Discussion in 'Android Questions' started by Enrico Fuoti, Sep 20, 2015.

  1. Enrico Fuoti

    Enrico Fuoti Member Licensed 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?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    All the network communication happens in the background (asynchronously). It will never pause your app flow.
     
  3. Enrico Fuoti

    Enrico Fuoti Member Licensed 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: Sep 21, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    The problem is somewhere else.
     
  5. marcick

    marcick Well-Known Member Licensed User

    are you sure your app hangs before JobDone and not in the following data elaboration ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    @marcick is most probably correct. What are you doing in JobDone? Can you post the code?
     
  7. Enrico Fuoti

    Enrico Fuoti Member Licensed User

    Yes,
    this is the Jobdone event..
    Code:
    '---------------------------
    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, 00)  
               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, 02) & " Bonus:" & li_bonus1 & " - " & li_bonus2 & " - " & li_bonus3 & " - " & li_bonus4 & "  Costo: " & NumberFormat(lf_costo1, 02)   & " - "  & NumberFormat(lf_costo2, 02) & " - "  & NumberFormat(lf_costo3, 02) & " - "  & NumberFormat(lf_costo4, 02)    & " - 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, 00)
               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, 00)
               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: Sep 21, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    Am I correct that you are not creating a transaction?

    Create a single transaction and it will be 1000 times faster.
     
  9. Enrico Fuoti

    Enrico Fuoti Member Licensed User

    Yes,
    This is the Jobdone event a bit shortened for readability,..

    Code:
    '---------------------------
    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, 00)  
               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: Sep 21, 2015
  10. Enrico Fuoti

    Enrico Fuoti Member Licensed 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?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

  12. marcick

    marcick Well-Known Member Licensed 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".
     
  13. Enrico Fuoti

    Enrico Fuoti Member Licensed 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
     
  14. marcick

    marcick Well-Known Member Licensed 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.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You will need to break the large job into smaller jobs (you can use a timer to download a smaller job each x seconds).
     
  16. Enrico Fuoti

    Enrico Fuoti Member Licensed 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...
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    You need to make the requests smaller => the server should return less data for each request.
     
  18. Enrico Fuoti

    Enrico Fuoti Member Licensed 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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice