Data Storage and Persistence

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Data Storage and Persistence

Post by physicsclassroom » Wed Aug 05, 2015 10:04 pm

I'm doing my first mobile app - a physics educational app for my students, my school, and whomever. I've read all I've could about data storage and persistence for mobile; its been enough to leave me confused. I have three challenges - one on which I've succeeded, one on which I'm failing, and one I have't tackled yet. I need help on the latter two.

Challenge #1 (success): I've successfully been able to write to a .txt file and retrieve information from session to session; this includes such as student name, ID, teacher name, teacher email, progress records, etc. I write/read the info on start-up and whenever necessary; it's working just like the docs say it should in the IDE, the simulators and on Android and iOS devices.

Challenge #2 (currently unsuccessful): Using nearly identical methods, I've tried to save the user state within the app to a file and then read that state and restore the state of the app at the time of "shut down" or when the user leaves the app and returns. So I have what seems to be called a "splash stack" or "launcher stack" that uses an on openStack handler on the first card to read from a .txt file and determine which stack and which card on the stack that the user was on when s/he left the stack. The method works in the IDE and restores the state of the app. However, it does not work on the iOS simulator or on an iOS device. The on openStack handler is executing, reading from the .txt file, and even determining the target stack and target card to which it should navigate. I'm using the close command to close the splash stack and the go to card targetCard of stack targetStack but the app stays on the first card of the Splash stack.

Challenge #3 (not attempted): The meat and potatoes of my app are assignments that consist of questions that the student must answer. There's as many as 40 questions appearing on their separate cards and grouped in as many as 10 groups of 4 questions. There is quite a bit of scripting taking place to determine which questions have been delivered (its random, requiring 1-2 correct answers from each group), which groups have been completed, which groups have been flagged by user to be included in a special review exercise, etc. Data is stored (transparent to the user) on the second card of the stack; there's a lot of data stored there. If the user leaves the app or quits in the middle of an assignment, it will be quite a task to save the state of the app. So I'm considering saving a copy of the stack to the documents folder when the assignment starts up and then just saving that stack on shutdown. On returning to the app, my splash stack can read from the usual .txt file and then open the stack from the documents folder. From what I've read, I can't tell if this is permitted on mobile devices. (And of course, I have to get #2 working before I can execute this plan ... or any other plan).

I'm having a related issue with all this such as, on shutDown handlers do not seem to be catching the message in the simulators and the devices when placed in stack scripts; I'm only getting them to work on card scripts. Thus, I'm writing state information to .txt files when navigating to a new stack or a new card within a stack.

I appreciate any help I can get as I am a real beginner. Thanks.
Last edited by physicsclassroom on Thu Aug 06, 2015 12:37 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Data Storage and Persistence

Post by dunbarx » Wed Aug 05, 2015 10:17 pm

Hi.

Regarding Challenge 2, May I assume that the stack into which you are trying to save your data is NOT the actual stack (or a substack) of the executable? That it is a completely separate stack in a completely separate file, attached to the executable via the Standalone Settings? Because if not, then that is your problem.

Craig Newman

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Wed Aug 05, 2015 10:42 pm

Hi Craig,

I'm saving my state info to a .txt file in the documents folder using URL ("file:shutDownState.txt"). And the main app stack (splash stack) is definitely reading the info and it is functioning as expected in the IDE - that is, restoring the state of the app. It's not working in the simulator or on the actual device. I've included some lines of script in the restoreState handler that reports the values stored in the .txt file and it is actually reporting the information the targetCard and targetStack correctly, just not navigating to the card on that stack.

Tom

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Data Storage and Persistence

Post by jacque » Thu Aug 06, 2015 7:44 pm

It sounds like a file path problem. After you issue the "go" command to go to the card of the saved state, add the line "answer the result". That may give you the reason for the failure. If it's "no such card" then the stack isn't being found, which is usually due to a bad file path. The fact that the splash stack doesn't close would indicate there's a script error that is aborting the rest of the handler.

I'm guessing you're using short stackfile names and the defaultFolder has been set to something other than the documents folder. I generally create full file paths dynamically rather than rely on the defaultFolder, which prevents this kind of error. If you can post the relevant part of the script we might be able to see if that's what's happening.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Storage and Persistence

Post by Klaus » Thu Aug 06, 2015 8:24 pm

Hi Tom,
physicsclassroom wrote:...I'm saving my state info to a .txt file in the documents folder using URL ("file:shutDownState.txt").
when using a relative pathname like in this case, the current defauktfolder will be used.
But that is probably the engine folder and you are not allowed to write there!
In that case the script silently fails, unless you check "the result" and act accordingly 8)

Use this:
... into URL ("file:" & specialfolderpath("documents") & "/shutDownState.txt")
and it will work fine :D

And also remember that iOS is casesensitive, so you need to take care of correct spelling of filenames:
shutDownState.txt <> shutDownstate.txt


Best

Klaus

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Thu Aug 06, 2015 9:35 pm

Hi Klaus,

Thanks for the feedback.
Klaus wrote: when using a relative pathname like in this case, the current DEFAULTfolder will be used.
But hat is probably the ENGINE folder and you are not allwoed to write there!
In that case the script silently fails, unless you check "the result" and act accordingly 8)
Before I make the call to the URL ("file:shutDownState.txt"), I'm using

set the defaultFolder to specialFolderPath("documents")

My use of lower/upper case seems to be OK and in fact I am retrieving the targetCard and targetStack from the .txt file. It's just that my go to statement isn't executing as I'm expecting it to. I'm going to try Jacque's suggestion and see what happens. Will post some code if there's still no breakthrough.

Tom

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Storage and Persistence

Post by Klaus » Thu Aug 06, 2015 9:46 pm

Hi Tom,
physicsclassroom wrote:...
Before I make the call to the URL ("file:shutDownState.txt"), I'm using
set the defaultFolder to specialFolderPath("documents")
ah, OK, I obviously missed that one.

Check "the result" after writing the file and let ANSWER the file content or whatever,
that often helps :D


Best

Klaus

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Thu Aug 06, 2015 10:03 pm

Hi Jacque and Klaus,

I added the answer the result line to the script after the go to command. The result was "OK". Yet the targetCard of the targetStack did not appear. The first card of the splash stack showed up. But I have a suspicion about what is going on and it probably brings me to the core of my confusion. There are some preOpenCard and preOpenStack scripts that are in the targetCard and targetStack that use some globals and do some housekeeping. I am going to guess that the intended results aren't being obtained because there's no values for the global variables.

So that brings me to this question: when a user is using my mobile app and leaves it to check their email or to text or to whatever, my app remains "open" in memory but does not return to the same state. Does this mean that the values of all globals are lost? Does restoring the state also mean that I need to restore the values of all globals? If so, then I have a lot more work to do. And that would also explain why everything works in the IDE; the IDE remembers the values of all globals? Could this be the problem?

Tom

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Data Storage and Persistence

Post by jacque » Thu Aug 06, 2015 11:39 pm

physicsclassroom wrote:I added the answer the result line to the script after the go to command. The result was "OK".
Well, if "OK" was in the text of the dialog prompt, that's wrong actually. A successful "go" command should give an empty result. If "OK" is the name of the button you see in the dialog, then it's working correctly and something other than the "go" command is erroring.
So that brings me to this question: when a user is using my mobile app and leaves it to check their email or to text or to whatever, my app remains "open" in memory but does not return to the same state. Does this mean that the values of all globals are lost? Does restoring the state also mean that I need to restore the values of all globals? If so, then I have a lot more work to do. And that would also explain why everything works in the IDE; the IDE remembers the values of all globals? Could this be the problem?
You can't depend on globals being retained on a mobile device. The OS will clear your app from RAM whenever it needs the memory for something else. It's a good idea to avoid the use of globals as much as possible, which is why it's better to explicitly define the file path as Klaus explained, rather than rely on a value that is only stored in memory. Your other globals will disappear too, so depending on what they contain, you should store those in the text file also. But even better is to re-define as many of those variables as possible dynamically in the scripts.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Thu Aug 06, 2015 11:56 pm

Thanks for your insight Jacque and Klaus. I guess I have a lot more work to do. I'll do some experimenting with storing variables in the .txt file and see if that helps solve this problem.

Tom

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Fri Aug 07, 2015 3:33 pm

Working on my solution now and a fearful thought comes to mind. I'm saving student progress records in a .txt file in the documents folder. This gets backed up and synced. Is it possible for a user of the app to access these files and change them ... thus quite quickly improving their progress?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Storage and Persistence

Post by Klaus » Fri Aug 07, 2015 3:53 pm

Hi Tom,

not sure this helps, but you can FLAG a file to NOT get backup-ed:
...
iphoneSetDoNotBackupFile FileName,TRUE
...

Best

Klaus

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Fri Aug 07, 2015 4:01 pm

Thanks Klaus.

Do I set that property that after the file with fileName is created? And once set, I don't have to ever re-set it?

Does Android OS have any type of backup/syncing that would put sensitive data at risk?

Tom

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Storage and Persistence

Post by Klaus » Fri Aug 07, 2015 4:18 pm

Hi Tom,
physicsclassroom wrote:Do I set that property that after the file with fileName is created? And once set, I don't have to ever re-set it?
not sure, the docs do not tell anything about this.
I would do this everytime I save or modify that namely file, will surely not hurt :D
physicsclassroom wrote:Does Android OS have any type of backup/syncing that would put sensitive data at risk?
Sorry, I have no idea.


Best

Klaus

physicsclassroom
Posts: 50
Joined: Sun Jun 30, 2013 1:43 am
Contact:

Re: Data Storage and Persistence

Post by physicsclassroom » Fri Aug 07, 2015 4:53 pm

Thanks again for all your help Klaus.

Post Reply