Read/Write/Restore Data
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Read/Write/Restore Data
I am having difficulty saving user progress with an iOS app.There are 14 substest that can be administered. I have two fields set up to save the subtest data / progress. I am saving to these fields as the user progresses, and then writing them to a text file. The problem seems to be on shutdown and startup. The text files are not always written. Also, even if they are the answer handler on start up isn't working. This is what I have so far:
on openStack - This is in the 1st card script to make sure it gets called. No other openStack handlers in the project.
if the environment is "mobile" then play specialFolderPath("engine") & "/audio/iossoundprep.mp3" --establish the path to eliminate any delays.
set the defaultFolder to specialFolderPath("Documents")
put URL ("file:geniepreferences.txt") into field "prefsField" of card "prefsCard" --User data file.
put URL ("file:markedcards.txt") into field "markedCardsField" of card "prefsCard" --List of the marked cards.
if card field "prefsField" of card "prefsCard" is not empty then answer "Continue Session?" with "Yes" or "No" --Not working.
if it is "Yes" then
restoreAllData --repopulate the data.
markTheCards --get's the saved list of marked cards and marks them.
put item 1 of line 58 of field "prefsField" of card "prefsCard" into tCurrentCard --name of the card the user exited on.
go to card tCurrentCard
else
send "clearData" to card "Data Card" in 20 --New session, clear all prior data.
put empty into card field "prefsField" of card "prefsCard" --Clear previous saved data.
put empty into card field "markedCardsField" of card "prefsCard" ----Clear previous saved data.
set the uText of control "Client Name Field" of card "PreferencesStartCard" to empty --MobGui text control.
put empty into card field "Name Field" of card "Data Card"
clearPictureDescr --Clears more data.
end if
end openStack
on shutdown - In the stack script; Have also tried closeStack.
global gCorrectAnswer, gAnswerCount, gcurrentPercentage, gFirstBtn, gLastBtn
put field "MLUfield" of card "Data Card" into item 1 of line 49 of field "prefsField" of card "prefsCard" -- data that can't be saved as the user progresses.
put gCorrectAnswer into item 1 of line 52 of field "prefsField" of card "prefsCard"
put gAnswerCount into item 1 of line 53 of field "prefsField" of card "prefsCard"
put gcurrentPercentage into item 1 of line 54 of field "prefsField" of card "prefsCard"
put gFirstBtn into item 1 of line 55 of field "prefsField" of card "prefsCard"
put gLastBtn into item 1 of line 56 of field "prefsField" of card "prefsCard"
put this card into item 1 of line 58 of field "prefsField" of card "prefsCard" --Save the current card so the user can resume if selected.
set the defaultFolder to specialFolderPath("Documents")
put field "prefsField" of card "prefsCard" into URL ("file:geniepreferences.txt") --Save the preferences / progress.
put field "markedCardsField" of card "prefsCard" into URL ("file:markedcards.txt") --Save the marked cards list.
end shutdown
This will be administered to special needs kids, so I need to be able to restore the session if they "accidentally" hit the home button. These kids are pretty smart with all things iPad. I will be glad to post additional scripts if it will be useful.
Thanks for you help!
on openStack - This is in the 1st card script to make sure it gets called. No other openStack handlers in the project.
if the environment is "mobile" then play specialFolderPath("engine") & "/audio/iossoundprep.mp3" --establish the path to eliminate any delays.
set the defaultFolder to specialFolderPath("Documents")
put URL ("file:geniepreferences.txt") into field "prefsField" of card "prefsCard" --User data file.
put URL ("file:markedcards.txt") into field "markedCardsField" of card "prefsCard" --List of the marked cards.
if card field "prefsField" of card "prefsCard" is not empty then answer "Continue Session?" with "Yes" or "No" --Not working.
if it is "Yes" then
restoreAllData --repopulate the data.
markTheCards --get's the saved list of marked cards and marks them.
put item 1 of line 58 of field "prefsField" of card "prefsCard" into tCurrentCard --name of the card the user exited on.
go to card tCurrentCard
else
send "clearData" to card "Data Card" in 20 --New session, clear all prior data.
put empty into card field "prefsField" of card "prefsCard" --Clear previous saved data.
put empty into card field "markedCardsField" of card "prefsCard" ----Clear previous saved data.
set the uText of control "Client Name Field" of card "PreferencesStartCard" to empty --MobGui text control.
put empty into card field "Name Field" of card "Data Card"
clearPictureDescr --Clears more data.
end if
end openStack
on shutdown - In the stack script; Have also tried closeStack.
global gCorrectAnswer, gAnswerCount, gcurrentPercentage, gFirstBtn, gLastBtn
put field "MLUfield" of card "Data Card" into item 1 of line 49 of field "prefsField" of card "prefsCard" -- data that can't be saved as the user progresses.
put gCorrectAnswer into item 1 of line 52 of field "prefsField" of card "prefsCard"
put gAnswerCount into item 1 of line 53 of field "prefsField" of card "prefsCard"
put gcurrentPercentage into item 1 of line 54 of field "prefsField" of card "prefsCard"
put gFirstBtn into item 1 of line 55 of field "prefsField" of card "prefsCard"
put gLastBtn into item 1 of line 56 of field "prefsField" of card "prefsCard"
put this card into item 1 of line 58 of field "prefsField" of card "prefsCard" --Save the current card so the user can resume if selected.
set the defaultFolder to specialFolderPath("Documents")
put field "prefsField" of card "prefsCard" into URL ("file:geniepreferences.txt") --Save the preferences / progress.
put field "markedCardsField" of card "prefsCard" into URL ("file:markedcards.txt") --Save the marked cards list.
end shutdown
This will be administered to special needs kids, so I need to be able to restore the session if they "accidentally" hit the home button. These kids are pretty smart with all things iPad. I will be glad to post additional scripts if it will be useful.
Thanks for you help!
Re: Read/Write/Restore Data
Well, assuming that the actions that your scripts will perform are correct...
Your answer command does not work as you have a badly formatted 'if' control structure
I have tidied up your script...
Try using 'closeCard' then the info you want saving will be saved everytime a card is closed...
be well
Dixie

Your answer command does not work as you have a badly formatted 'if' control structure
I have tidied up your script...
Try using 'closeCard' then the info you want saving will be saved everytime a card is closed...
Code: Select all
global gCorrectAnswer, gAnswerCount, gcurrentPercentage, gFirstBtn, gLastBtn
on openStack
if the environment is "mobile" then
set the defaultFolder to specialFolderPath("Documents")
play specialFolderPath("engine") & "/audio/iossoundprep.mp3"
put URL ("file:geniepreferences.txt") into field "prefsField" of card "prefsCard"
put URL ("file:markedcards.txt") into field "markedCardsField" of card "prefsCard"
if card field "prefsField" of card "prefsCard" is not empty then
answer "Continue Session?" with "Yes" or "No"
if it is "Yes" then
restoreAllData
markTheCards
put item 1 of line 58 of field "prefsField" of card "prefsCard" into tCurrentCard
go to card tCurrentCard
else
send "clearData" to card "Data Card" in 20
put empty into card field "prefsField" of card "prefsCard"
put empty into card field "markedCardsField" of card "prefsCard"
set the uText of control "Client Name Field" of card "PreferencesStartCard" to empty
put empty into card field "Name Field" of card "Data Card"
clearPictureDescr
end if
end if
end if
end openStack
on closeCard
set the defaultFolder to specialFolderPath("Documents")
put field "MLUfield" of card "Data Card" into item 1 of line 49 of field "prefsField" of card "prefsCard"
put gCorrectAnswer into item 1 of line 52 of field "prefsField" of card "prefsCard"
put gAnswerCount into item 1 of line 53 of field "prefsField" of card "prefsCard"
put gcurrentPercentage into item 1 of line 54 of field "prefsField" of card "prefsCard"
put gFirstBtn into item 1 of line 55 of field "prefsField" of card "prefsCard"
put gLastBtn into item 1 of line 56 of field "prefsField" of card "prefsCard"
put this card into item 1 of line 58 of field "prefsField" of card "prefsCard"
put field "prefsField" of card "prefsCard" into URL ("file:geniepreferences.txt")
put field "markedCardsField" of card "prefsCard" into URL ("file:markedcards.txt")
end closeCard
Dixie
Re: Read/Write/Restore Data
That was it! Thank you very much! You were also correct about the formatting. I can't believe I wrote it that way. I know that I was pasting lines, but I should have caught it.
Re: Read/Write/Restore Data
Hi guys,
some handy hints
@ncmac
Plase use the "Code" button to format your LiveCode scripts "as is" after pasting the code here, that'll make the code readable!
@all
First check conditions that may result in leaving the handler, that'll make the code also much more readable!
Best
Klaus
some handy hints

@ncmac
Plase use the "Code" button to format your LiveCode scripts "as is" after pasting the code here, that'll make the code readable!

@all
First check conditions that may result in leaving the handler, that'll make the code also much more readable!
Code: Select all
on openStack
if the environment <> "mobile" then
exit openstack
end if
## One "IF... END IF" less :-)
## Rest goes here...
end openstack
Klaus
Re: Read/Write/Restore Data
Thanks for pointing out the Code button. BTW, <> doesn't return anything in the dictionary. Can you elaborate on it?
Re: Read/Write/Restore Data
That stands for "is not"!
There is a character for that ≠, but that is not crossplatform, so I got used to <> (and <= and >=)
There is a character for that ≠, but that is not crossplatform, so I got used to <> (and <= and >=)

Re: Read/Write/Restore Data
Thanks! Can you help me with one more thing? 
Why would an openCard handler start before the actual screen transition has completed? This is the openStack Handler:
It works great except when the user has resumed (ie tapped "Yes") the openCard speech file is played about 1 or 2 seconds before the screen changes to tCurrentCard.

Why would an openCard handler start before the actual screen transition has completed? This is the openStack Handler:
Code: Select all
on openStack
if the environment is "mobile" then
set the defaultFolder to specialFolderPath("Documents")
play specialFolderPath("engine") & "/audio/iossoundprep.mp3"
put URL ("file:geniepreferences.txt") into field "prefsField" of card "prefsCard"
put URL ("file:markedcards.txt") into field "markedCardsField" of card "prefsCard"
if card field "prefsField" of card "prefsCard" is not empty then
answer "Continue Session?" with "Yes" or "No"
if it is "Yes" then
restoreAllData
markTheCards
put item 1 of line 58 of field "prefsField" of card "prefsCard" into tCurrentCard
go to card tCurrentCard
else
send "clearData" to card "Data Card" in 20
put empty into card field "prefsField" of card "prefsCard"
put empty into card field "markedCardsField" of card "prefsCard"
set the uText of control "Client Name Field" of card "PreferencesStartCard" to empty
put empty into card field "Name Field" of card "Data Card"
send "clearPictureDescr" to me in 20
go to card "PreferencesStartCard"
end if
end if
end if
end openStack
Re: Read/Write/Restore Data
Slightly off-topic by in iOS 6 you'll be able to prevent users exiting apps with the Home button.
Gerry
Gerry
14" MacBook Pro
Former LiveCode developer.
Now recovering.
Former LiveCode developer.
Now recovering.