Android Question problems with b4a 6.3

billyrudi

Active Member
Licensed User
Longtime User
Hi, i think that there are problems while looping over a cursor with sqllite.... it is slow slow slow.
someone has encountered the same problem?
today 5 seconds
yesterday before new version less then 1 second
regards Paolo

B4X:
Dim Cursor1 As Cursor
    Dim ssell As String
    If lstelencoGiri.IsInitialized = False Then
        Return
    End If
    lstelencoGiri.Clear1

    ssell = "Select min(Esito) Esito, CodiceDestinatario,max(RagioneSociale) RagioneSociale , max(Indirizzo) Via,max(Citta) Citta, max(IDTransazione) IDTransazione  from Viaggi  where IDFoglioDiViaggio = '" & Operazioni.idfoglioviaggio & "' and Filiale = '" & Operazioni.siglaFiliale & "' group by CodiceDestinatario  Order by Ordinamento Asc"
    Cursor1 = Operazioni.SQL1.ExecQuery( ssell)
    Dim ik As Int
    ik = 0
    Dim bpassato As Boolean
    Dim bgiallo As Boolean
    bpassato = False
    Dim colore As Int
    Dim ultimob As Int
    Operazioni.girifatti = 0
    Operazioni.giritotale = Cursor1.RowCount
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        'individuazione tipo colore
    
        If Cursor1.GetString("Esito") = "S" Or  Cursor1.GetString("Esito") = "X" Then    
            colore = Colors.RGB(6,129,35)
            bpassato = True
            bgiallo = True
            ik = i
            Operazioni.girifatti = Operazioni.girifatti +1
        Else
            If Cursor1.GetString("Esito") = Null Then
                colore  = Colors.White
                If  bpassato = False Then
                    bpassato = True
                    ik = i
                End If
                ultimob = i
            Else
               If Cursor1.GetString("Esito") = "I" Then
                    colore = Colors.Yellow
                    bpassato = True
                    bgiallo = True
                    ik = i
                Else
                    colore = Colors.Red
                    bpassato = True
                    bgiallo = True
                    ik = i
                End If
            
             End If
        End If
    
    
        Dim sfondo As Int
        If i Mod 2 = 0 Then
            sfondo = Colors.Black
        Else
         sfondo =    Colors.DarkGray
        End If
        Dim indices As String
        indices = i + 1
        Dim ordinamento As Int
    
        ordinamento = Cursor1.GetInt("CodiceDestinatario")
        Dim ssel, scons, srit As String
        ssel = "Select count(RagioneSociale) tot from Viaggi where TipoServizio = 'C'  and  IDFoglioDiViaggio = '" & Operazioni.idfoglioviaggio & "' and Filiale = '" & Operazioni.siglaFiliale & "' and CodiceDestinatario = '" & Cursor1.GetString("CodiceDestinatario") & "' group by CodiceDestinatario"
        Dim ct As Cursor
        ct  = Operazioni.SQL1.ExecQuery(ssel)
        If ct.RowCount > 0 Then
            ct.Position = 0
            scons = " - N. Cons.: "& ct.GetInt ("tot")
        Else
            scons = " N. Cons.: 0"
        End If
        ct.close
        ssel = "Select count(RagioneSociale) tot from Viaggi where TipoServizio = 'R' and  IDFoglioDiViaggio = '" & Operazioni.idfoglioviaggio & "' and Filiale = '" & Operazioni.siglaFiliale & "' and CodiceDestinatario = '" & Cursor1.GetString("CodiceDestinatario") & "' group by CodiceDestinatario"
    
        ct  = Operazioni.SQL1.ExecQuery(ssel)
        If ct.RowCount > 0 Then
            ct.Position = 0
            srit = " - N. Rit.: "& ct.GetInt ("tot")
        Else
            srit = " - N. Rit.: 0"
        End If
        ct.Close
        lstelencoGiri.Add(CreateListaItem(indices & ") " &  Cursor1.GetString("RagioneSociale")  & CRLF & Cursor1.GetString("Via") & " " & Cursor1.GetString("Citta") & scons & srit, lstelencoGiri.AsView.Width,   35dip,colore,sfondo, Cursor1.GetString("IDTransazione")), 60dip , ordinamento) 
    Next
    Cursor1.Close
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you testing it with the project you've uploaded? There are just two items and they appear immediately.

You need to add code that measures the time it takes for the slow step and post the logs:
B4X:
Dim n As Long = DateTime.Now
..code that you suspect is slow
Log("Took: " & (DateTime.Now - n))
Make sure to test it in release mode.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem is in this code:
B4X:
For i = 0 To Cursor1.RowCount - 1
Cursor1.Position = i
'individuazione tipo colore

If Cursor1.GetString("Esito") = "S" Or  Cursor1.GetString("Esito") = "X" Then       
    colore = Colors.RGB(6,129,35)
    bpassato = True
    bgiallo = True
    ik = i
    Operazioni.girifatti = Operazioni.girifatti +1
Else
    If Cursor1.GetString("Esito") = Null Then   
        colore  = Colors.White
        If  bpassato = False Then
            bpassato = True
            ik = i
        End If
        ultimob = i
    Else
        If Cursor1.GetString("Esito") = "I" Then
            colore = Colors.Yellow
            bpassato = True
            bgiallo = True
            ik = i
        Else
            colore = Colors.Red
            bpassato = True
            bgiallo = True
            ik = i
        End If
    End If
End If

I replaced by this and it works:
B4X:
If Cursor1.GetString("Esito") = Null Then   
    colore  = Colors.White
    If  bpassato = False Then
        bpassato = True
        ik = i
    End If
    ultimob = i
Else
    If Cursor1.GetString("Esito") = "S" Or  Cursor1.GetString("Esito") = "X" Then       
        colore = Colors.RGB(6,129,35)
        bpassato = True
        bgiallo = True
        ik = i
        Operazioni.girifatti = Operazioni.girifatti +1
    Else If Cursor1.GetString("Esito") = "I" Then
        colore = Colors.Yellow
        bpassato = True
        bgiallo = True
        ik = i
    Else
        colore = Colors.Red
        bpassato = True
        bgiallo = True
        ik = i
    End If                   
End If

I had the same problem as Erel with a black screen.
Then I put a breakpoint at the beginning of the routine and got the result at the first run without a click on rubrica and on viaggi.
The I removed the Try / Catch and got this error
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
in this line:
If Cursor1.GetString("Esito") = Null Then
And changed the code.

There are many warnings in your code, you should have a look at those.
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
UAUUUU,
i have test it and now works perfectly!
But. why in debug i'm not able to see it?
now i'm happy! tank you very very mutch:)
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Because of the Try / Catch.

Klaus, good Catch (pun intended;)).

Let that be a lesson to all of us. Inside of a Try/Catch block you should at least do a "Log(LastException)" so you know these exceptions are occurring.

In Delphi if the app it is run from the IDE in debug mode and an exception occurs, it stops at the line that caused the exception (even if it is inside a Try/Except/Finally block) as if it were a breakpoint and displays the exception message. The user can then step through the code or continue running the app from where it stopped. You can also have a list of exceptions to ignore (or ignore all of them) in the debugger so the IDE won't stop when it encounters these exceptions.
 
Upvote 0
Top