Android Question KeyValueStore2 and Android Things

fbritop

Active Member
Licensed User
I could not find where my AT app got stucked, after putting a lot of LOG Flags, I found out that

B4X:
Public Sub Put(Key As String, Value As Object)
And KVS2 Class takes an enormous ammount of time to execute (from 3 to 7 seconds) to store a single Integer Value. But usualy it stores a small JSON Map

I cannot find out if the problem is with SQL or B4XSerializator.

Is there any precautions I should take when using KVS2 under RPI with AT?

Thanks
FBP
 
Last edited:

fbritop

Active Member
Licensed User
Erel,
Before your request i did some tests yesterday. I somehow managed to narrow down the problem to the line

B4X:
sql1.ExecNonQuery2("INSERT OR REPLACE INTO main VALUES(?, ?)", Array (Key, ser.ConvertObjectToBytes(Value)))
Putting your same logic of dateTime.now differences. That was the point where de delay was. I thought it could be related to B4XSerializator so I changed it for testing to

B4X:
sql1.ExecNonQuery2("INSERT OR REPLACE INTO main VALUES(?, ?)", Array (Key, Value))
With the same results. Then it was SQL, or somehow write disk operations. I'm an advance SQL Server user, but I have no clues about efficiency or performance regarding SQLLite, but I changed INSERT OR REPLACE and the problem was solved. By code now looks as:

B4X:
    Try
        Dim ahora As Long = DateTime.Now
        
        Dim rs As ResultSet = sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
        If rs.NextRow Then
            sql1.ExecNonQuery2("update main set value=? where key=?", Array (Key, ser.ConvertObjectToBytes(Value)))
        Else   
            sql1.ExecNonQuery2("INSERT INTO main VALUES(?, ?)", Array (Key, ser.ConvertObjectToBytes(Value)))
        End If
        rs.Close
        'sql1.ExecNonQuery2("INSERT OR REPLACE INTO main VALUES(?, ?)", Array (Key, ser.ConvertObjectToBytes(Value)))
        Log("KEY:" & Key & " " & (DateTime.Now-ahora))
    Catch
        Log("KVS:ERROR.PUT:" & Key)
        Log(LastException)
    End Try
Not shure where the bookneck is whith SQLLite
 

OliverA

Expert
Licensed User
ql1.ExecNonQuery2("update main set value=? where key=?", Array (Key, ser.ConvertObjectToBytes(Value)))
There is a slight logic error here. Your array needs to be
B4X:
Array (ser.ConvertObjectToBytes(Value), Key)
Update: That may change your timing again, since before (with the original array) it never performed any updates
 

fbritop

Active Member
Licensed User
What is the output of:
B4X:
Dim n As Long = DateTime.Now
kvs2.Put("test", 100)
Log($"Time1: ${DateTime.Now - n}"$")
n = DateTime.Now
File.WriteString(File.DirInternal, "test", "test")
Log($"Time2: ${DateTime.Now - n}"$")
?
Time1: 1334
Time2: 1
 

fbritop

Active Member
Licensed User
Narrow down the problem, it only does happen when in the same sub there is a Timer Enable True, even thow I use Sleep(0). I use a lot of timers because of 8 different relays that I need to control
 
Top