Android Question Is Keyvaluestore can be inbeded in the final app?

Yves Mazzon

Member
Licensed User
Longtime User
I'm using this line in my app:
kvs.Initialize(File.DirInternal,"values_store")

It works well but only if the file exist already. But I realized if the first time user start the app android comes with invalid double errors;"" as there is no values file yet created. My question is will it be possible to populate the data in the app.apk so the very first time the application is open it will have all the numeric variables and strings populated, avoiding the error in the first place? Is anything to do with File.DirAssets,... than what I am using File.DirInternal... alternative?
Many thanks

Yves
 

Yves Mazzon

Member
Licensed User
Longtime User
Thank you Erel but is kvs.ContainsKey is checking the file exist or if there is anything in it? in my application I use something like this:
kvs.Initialize(File.DirInternal,"values_store") 'open a file called values_store
If kvs.ContainsKey("values_store") = False Then
"I populate the labels so we don't get an error"
end if
Then I get the
Sub Activity_Pause (UserClosed As Boolean) to save all the user new data and Sub Activity_Resume to retrieve the last input data when the app is reopened.
These work fine but my If kvs.ContainsKey("values_store") = False is always false no matter if the "value_store" file exist or not.
I need this to work because on the first use of the application or if the cache are erased otherwise I get an java error because the labels are blank and parsing empty strings. Maybe I am not understanding the kvs.ContainsKey function. Many thanks.
Regards,
Yves
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
I did something like this in an app I am creating
B4X:
kvs.Initialize("value_store")

'Check if value_store contains a version key.  If not, then it means this is a new file
If kvs.ContainsKey("version") = false Then
     'Populate all the keys with default values
     kvs.PutSimple("highscore",0)
     kvs.PutSimple("difficulty","easy")
     kvs.PutSimple("version",Application.VersionCode)
End If

'Check to see if value store version matches app version.  Upgrade the keys if value_store
'is lower.
If kvs.GetSimple("version") < Application.VersionCode Then
    kvs.PutSimple("NewFeature",10)
    kvs.PutSimple("version",Apllication.VersionCode)
End If

'Now we can read in all the data and set the values
Dim HighScore As Int = kvs.GetSimple("highscore")
Dim Difficulty as String = kvs.GetSimple("difficulty")
'etc...
 
Upvote 0

Yves Mazzon

Member
Licensed User
Longtime User
Great James, that will do for me. I will add something like DateTime.now as a key and if the string is empty the program will know the file doesn't exist then populate all the keys with default values. Great stuff many thanks.
Regards,

Yves
 
Upvote 0
Top