Read/Write/Restore Data

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ncmac
Posts: 31
Joined: Thu Apr 27, 2006 10:29 pm

Read/Write/Restore Data

Post by ncmac » Sun Jun 10, 2012 11:18 pm

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!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Read/Write/Restore Data

Post by Dixie » Mon Jun 11, 2012 1:01 am

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...

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
be well

Dixie

ncmac
Posts: 31
Joined: Thu Apr 27, 2006 10:29 pm

Re: Read/Write/Restore Data

Post by ncmac » Wed Jun 13, 2012 5:30 pm

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.

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

Re: Read/Write/Restore Data

Post by Klaus » Wed Jun 13, 2012 7:25 pm

Hi guys,

some handy hints :D

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

@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 
Best

Klaus

ncmac
Posts: 31
Joined: Thu Apr 27, 2006 10:29 pm

Re: Read/Write/Restore Data

Post by ncmac » Thu Jun 14, 2012 7:05 pm

Thanks for pointing out the Code button. BTW, <> doesn't return anything in the dictionary. Can you elaborate on it?

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

Re: Read/Write/Restore Data

Post by Klaus » Thu Jun 14, 2012 7:26 pm

That stands for "is not"!
There is a character for that ≠, but that is not crossplatform, so I got used to <> (and <= and >=) :D

ncmac
Posts: 31
Joined: Thu Apr 27, 2006 10:29 pm

Re: Read/Write/Restore Data

Post by ncmac » Fri Jun 15, 2012 2:29 am

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:

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
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.

Jellicle
Posts: 453
Joined: Thu Feb 24, 2011 11:07 am

Re: Read/Write/Restore Data

Post by Jellicle » Fri Jun 15, 2012 7:51 am

Slightly off-topic by in iOS 6 you'll be able to prevent users exiting apps with the Home button.

Gerry
14" MacBook Pro
Former LiveCode developer.
Now recovering.

Post Reply