Android Question StateManager of KeyValueStore for saving user preferences

beachner

Member
Hi
What is the recommended way of saving user preferences? I have an app that requires the user to download some settings for the app to work properly. At the moment I'm using KeyValueStore but occasionally it appears that it fails. I don't know why but sometimes it seems like KeyValueStore fails to read certain values which makes the app stop working correctly and the only way to get it to work is to download the configuration again.
Should I switch to StateManager for a more stable implementation or is there an option I have missed?
 

beachner

Member
That's the problem, we don't have any error messages. We have never seen it actually happen, only the results when our customers contact us to tell us that the application is not functioning properly.
We have quit complex application that is dependent on a working configuration and occasionally this configuration fails us and the application stops functioning correctly. What leads me to believe it has something to do with KeyValueStore is that as soon as we reapply the complete configuration the application start functioning correctly. It is quite possible that it is how we use the KeyValueStore that is the problem. However, we read from the KeyValueStore in a lot of different places and depending on what key we fail to read we get different incorrect behaviours.

At the moment the KeyValueStore is accessed from a Utils-class that is initialized in various different classes and we both write and read to the KeyValueStore in numerous places. Could this be the problem?

How would you recommend we should use the KeyValueStore in an effienct and safe way? Is it possible to create a singleton in B4A?

The initialisation code is run in the initialisation function of the Utils-class
Initialization:
    xui.SetDataFolder("kvs")
    KVS.Initialize(xui.DefaultFolder,"kvs.data")

Write:
Public Sub WriteConfig(mykey As String,myvalue As String)
    Try
        KVS.Put(mykey,myvalue)
    Catch
       Logger("Failed to write value " & myvalue & " for key " & mykey & ". Exception: " & LastException)
        Log(LastException)
    End Try
End Sub

Read config:
Public Sub ReadConfig(key As String) As String
    Try
        If KVS.ContainsKey(key) Then
            Return KVS.Get(key)
        End If
    Catch
        Logger("Failed to load value for key " & key & ". Exception: " & LastException)
        Log(LastException)
    End Try
    Return ""
End Sub

The logging code you see in the read and write methods have just been added and have not yet gone out to any customers so no info from there I'm afraid.
 
Upvote 0
Top