Data Storage and Persistence
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Data Storage and Persistence
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.
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.
Re: Data Storage and Persistence
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
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
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
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
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
Re: Data Storage and Persistence
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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: Data Storage and Persistence
Hi Tom,
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
Use this:
... into URL ("file:" & specialfolderpath("documents") & "/shutDownState.txt")
and it will work fine
And also remember that iOS is casesensitive, so you need to take care of correct spelling of filenames:
shutDownState.txt <> shutDownstate.txt
Best
Klaus
when using a relative pathname like in this case, the current defauktfolder will be used.physicsclassroom wrote:...I'm saving my state info to a .txt file in the documents folder using URL ("file:shutDownState.txt").
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

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

And also remember that iOS is casesensitive, so you need to take care of correct spelling of filenames:
shutDownState.txt <> shutDownstate.txt
Best
Klaus
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
Hi Klaus,
Thanks for the feedback.
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
Thanks for the feedback.
Before I make the call to the URL ("file:shutDownState.txt"), I'm usingKlaus 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![]()
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
Re: Data Storage and Persistence
Hi Tom,
Check "the result" after writing the file and let ANSWER the file content or whatever,
that often helps
Best
Klaus
ah, OK, I obviously missed that one.physicsclassroom wrote:...
Before I make the call to the URL ("file:shutDownState.txt"), I'm using
set the defaultFolder to specialFolderPath("documents")
Check "the result" after writing the file and let ANSWER the file content or whatever,
that often helps

Best
Klaus
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
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
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
Re: Data Storage and Persistence
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.physicsclassroom wrote:I added the answer the result line to the script after the go to command. The result was "OK".
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.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?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
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
Tom
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
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?
Re: Data Storage and Persistence
Hi Tom,
not sure this helps, but you can FLAG a file to NOT get backup-ed:
...
iphoneSetDoNotBackupFile FileName,TRUE
...
Best
Klaus
not sure this helps, but you can FLAG a file to NOT get backup-ed:
...
iphoneSetDoNotBackupFile FileName,TRUE
...
Best
Klaus
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
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
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
Re: Data Storage and Persistence
Hi Tom,
I would do this everytime I save or modify that namely file, will surely not hurt
Best
Klaus
not sure, the docs do not tell anything about this.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?
I would do this everytime I save or modify that namely file, will surely not hurt

Sorry, I have no idea.physicsclassroom wrote:Does Android OS have any type of backup/syncing that would put sensitive data at risk?
Best
Klaus
-
- Posts: 50
- Joined: Sun Jun 30, 2013 1:43 am
- Contact:
Re: Data Storage and Persistence
Thanks again for all your help Klaus.