Page 1 of 1

Saving data on Android

Posted: Sun Jan 14, 2018 12:55 pm
by DolphinTech
Hello everyone, I have got a new question concerning how to save data (e.g. Highscore) on a android device.
I currently use

Code: Select all

put highscore into url("file:"&specialFolderPath("cache")&"/highscore")
, which works for me.
But is this method "clean" and are there any conventions about this topic? And where exactly is this file saved (i couldn't find it on my phone)?

Another problem is that I cannot save array variables in files. Something like

Code: Select all

put 10 into stats["highscore"]
put stats into url("file:"&specialFolderPath("cache")&"/highscore")
produces an empty file. Are there any ways to fix this or is this a bug.

Thank you

Re: Saving data on Android

Posted: Sun Jan 14, 2018 1:14 pm
by sphere
put your highscore field or whatever in a variable like tHS
then save this variable to a textfile

put tHS into URL ( "file:" & specialFolderPath("Documents") & "/highscore.txt" )

then read out again
put specialFolderPath("Documents") & "/highscore.txt" into tWhatwasit
if there is a file tWhatwasit then
put url("file:" & ttWhatwasit) into toHthis
answer toHthis

Re: Saving data on Android

Posted: Mon Jan 15, 2018 5:39 pm
by Klaus
Hi DolphinTech,

arrays are binary data, so use the arrayEncode/arrayDecode to save and retrieve array data in text format:
...
put 10 into stats["highscore"]
## Store array in TXT format:
put arrayEncode(stats) into url("file:" & specialFolderPath("documents") & "/highscore")
...
## Read into variable again:
put url("file:" & specialFolderPath("documents") & "/highscore") into tStats
put arrayDecode(stats) tStats
...
Hint:
For persistent data use specialfolderpath("documents")!


Best

Klaus

Re: Saving data on Android

Posted: Mon Jan 15, 2018 6:58 pm
by DolphinTech
Thank you Klaus for your reply.
Obviously, arrayEncode was the function I was looking for.
But is the data stored into specialFolderPath("cache") not persistent?
And can data stored into "documents" be edited easily by others?

Thanks
-DolphinTech

Re: Saving data on Android

Posted: Mon Jan 15, 2018 7:20 pm
by Klaus
Hi DolphinTech,
DolphinTech wrote:
Mon Jan 15, 2018 6:58 pm
But is the data stored into specialFolderPath("cache") not persistent?
it is, but that folder is not being backed up by iTunes, if that is important for you.
DolphinTech wrote:
Mon Jan 15, 2018 6:58 pm
And can data stored into "documents" be edited easily by others?
Everyone who has access to the device may be able to edit the data.
So maybe use encrypt() to encrypt your data before saving, if you have really sensitive data.


Best

Klaus

Re: Saving data on Android

Posted: Mon Jan 15, 2018 7:46 pm
by DolphinTech
I don't have a Mac, so I can't deploy for iOS anyways and that makes the iTunes irrelevant for me.
if you have really sensitive data.
My data has the relevance of a single-player-offline-mobile-game-progress, but i don't want it to be editable with just a text editor.

I could not find the data stored by specialFolderPath("cache") on my phone. Do you know where it is saved?

Thanks

-DolphinTech

Re: Saving data on Android

Posted: Mon Jan 15, 2018 7:56 pm
by Klaus
DolphinTech wrote:
Mon Jan 15, 2018 7:46 pm
I could not find the data stored by specialFolderPath("cache") on my phone. Do you know where it is saved?
Sorry, no idea! Just add a button and check it by yourself on the device:
...
answer specialfolderpath("cache")
...
:D

Re: Saving data on Android

Posted: Mon Jan 15, 2018 8:06 pm
by DolphinTech
Did this.
the path is /data/user/0/com.DolphinTech.minesort/cache
I could not find this path on my phone. any reasons why?

Re: Saving data on Android

Posted: Mon Jan 15, 2018 8:16 pm
by sphere
i must have misread the first topic...

Re: Saving data on Android

Posted: Tue Jan 16, 2018 7:40 pm
by jacque
The cache folder is not permanent. Data will remain until the OS does housekeeping, or the user clears the cache to regain disk space. You can't rely on those files being there, the cache is meant for temporary data. The documents folder is permanent and never cleared until the user uninstalls the app.

Both the cache and documents folders are stored inside the app sandbox and cannot be accessed normally. Those files are not displayed by a file manager and cannot be seen or edited by users. The exception is if the device has been rooted. If so, all files are available. But most users do not have rooted phones so the documents folder is as secure as it gets.

Re: Saving data on Android

Posted: Sun Jan 21, 2018 12:29 pm
by DolphinTech
Thank you. I will consider using the "documents" folder.

-P.S. how do I close a topic?