Android Question [SOLVED] KeyValueStore : missing key // default values

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hi,
I am trying to find a way to handle missing keys in KeyValueStore. I had created a weekly scheduler that worked till the user deleted the file where the schedules were stored.

Perhaps did I not well understood the problem but, it seems to me (unsure) that if I initialize kvs in the Activity_Create, the file is created. So I can not check if file.exists

So I have tried to do a try/catch to be able to populate by default, but, as I have seen, if a key is missing the try/catch doesn't handle the code in the code module, or does it ?

Here is my structure :
keys : string values from 1 to 7 for each day of week
object : a type with two strings for the start time and the end time (HH:MM)

The workaround I have found is to use a Map inside a defined key ("scheduled") to be able to use the GetDefault. But I really wonder if I don't do it too difficult. Perhaps do you have any other solution ?

Thanks
 

LucaMs

Expert
Licensed User
Longtime User
Could you add a field to your UDT? For example, a boolean for ON/OFF?


In addition, KeyValueStore is a class which you have the source code and you can modify it.

You could modify the initialization with File.Exists.

I've added a method almost identical to GetSimple:
B4X:
' Similar to GetSimple but it creates the Key field,
' if it does not already exists, writing the Default value.
Public Sub GetSimple2(Key As String, Default As Object) As String
    Dim c As Cursor = getCursor(Key)
    If c.RowCount = 0 Then
        c.Close
        PutSimple(Key, Default)
        Return Default
    End If
    c.Position = 0
    Dim res As String = c.GetString2(0)
    c.Close
    Return res
End Sub
 
Last edited:
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
I am so stupid : I have found another solution by creating the line when kvs returns null. Leaving it here for an idea if someone as the same issue.


B4X:
' Sub inside the activity where the type is accessed - if the line or the file does not exist, they are created on the fly

Sub MySub
Dim tempRank As String
    For dayLoop=1 To 7
      tempRank=dayLoop
      Dim myLine As TypeI ' here is a type with the status of the scheduled line and start + end hours
      myLine.Initialize
      kvs.Initialize(File.DirInternal, "schedule.db") 
      myLine=kvs.GetEncryptedObject(tempRank,myPass)
     
        If myLine=Null Then
           FixLine(tempRank)
           Exit
        Else
             'Do Something
        End If
     Next
End Sub

Sub FixLine(rank as String)
  Dim myLine As TypeI ' the same type as before
  myLine.Initialize  
  myLine.status=False
  myLine.start="00:00"
  myLine.end="00:00"

  ' kvs.Initialize(File.DirInternal, "schedule.db")
  kvs.PutEncyptedObject(rank,myLine,myPass)
  MySub
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…