Save data or custom properties

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Save data or custom properties

Post by sirobchGA0cda » Sun Jan 26, 2014 4:53 pm

Hello,
I will translate my app with the custom properties in the stack "custom properties.
In OSX it's working.
In IOS when the program is closed and then relaunch, the last properties are not maintained.
I search for several days for a solution, i have read the forum and the tutorials on the web.
I do not understand how you must do to save the data or properties
Here is the code for my stack :

Code: Select all

global tDataStackPath
on preOpenStack
   put specialFolderPath("documents") into tDataStackPath
   put tDataStackPath & "/data.livecode" into tDataStackPath
      
   if not (there is a file tDataStackPath) then
      create stack "myDataStack"
      

      answer "Create stack:" && the result
      put it into temp
      set the visible of temp to false
      set the filename of temp to tDataStackPath
      save temp
      answer "Save stack:" && the result


## etc. Until you see WHERE it fails...

      go stack "MyMainStackNameHere"
   end if
   restoreMyCardFromDataStack // i will come to this in a moment
end preOpenStack




command saveMyCardToDataStack
   copy card "myCard" of this stack to stack tDataStackPath
   delete the first card of stack tDataStackPath
end saveMyCardToDataStack

command restoreMyCardFromDataStack
   copy card "myCard" of stack tDataStackPath to this stack
   delete the first card of this stack
end restoreMyCardFromDataStack

on shutdown
   saveMyCardToDataStack
   save stack tDataStackPath
end shutdown
and the code of one button :

Code: Select all

global tDataStackPath
on mouseUp
   set the customPropertySet of this stack to "Anglais"
   set the label of btn "b1" of cd 1 to the b1 of this stack
end mouseUp
Thank in advance for your answer.
Best regards
Boris
My application "GeoMaths" is available on Itunes and Google Play !

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Save data or custom properties

Post by jmburnod » Sun Jan 26, 2014 9:14 pm

Hi Boris,

Good to heard you here.
In IOS when the program is closed and then relaunch, the last properties are not maintained
Standalone remains as you compile it. For OSX too
I use ".txt" file to save data on shutdown or when that is useful and I import .txt file in custom properties on preopenstack
Kind regards
Jean-Marc
https://alternatic.ch

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Save data or custom properties

Post by Simon » Sun Jan 26, 2014 9:22 pm

There really should be a big note in the front of the docs which says:
"Standalones cannot save data to themselves but saving data to an external file one can replicate the behaviour."

This question comes up so often.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Save data or custom properties

Post by jacque » Sun Jan 26, 2014 9:39 pm

In this case though the OP is saving the data to a preferences stack, not to the app stack, so it should work. The prefs stack is in the documents folder, which is writable.

I question whether a whole card needs to be copied back and forth just to save some properties, but it should work anyway. I have used text files to save values in the past and that works very well but I mostly save properties in a prefs stack now because then I can refer to properties directly in the prefs stack without needing to import anything. I just open the prefs on startup, retrieve and/or save everything there, and close it on shutdown.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Mon Jan 27, 2014 7:35 pm

Hi all,
thanks for your answers.
I think I understand where is my mistake.
I did not insert the correct data in some settings in the Stack script.
I have testing with two card and the variables are maintained.
I am trying to get there and especially to understand the logic, which is not easy at first. :roll:

Best regards
Boris
My application "GeoMaths" is available on Itunes and Google Play !

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Sat Feb 01, 2014 10:29 am

Hello,
Now the variables are maintained, but with a little problem :
1°on the first launching In the simulator, i launch the app, then close the app, and i relaunch the app the variables are not maintained.

2° i relaunch the app and and close the app, and now the variables are maintained.

I don't understand why on the first launch and close is not working ?

Code: Select all

global tDataStackPath --MARK mise en mémoire
on preOpenStack
      put specialFolderPath("documents") into tDataStackPath
      put tDataStackPath & "/data.livecode" into tDataStackPath
         
      if not (there is a file tDataStackPath) then
            create stack "myDataStack"
            put it into temp
            set the visible of temp to false
            set the filename of temp to tDataStackPath
            save temp
            go stack "MyMainStackNameHere"
      end if
      restoreMyCardFromDataStack // i will come to this in a moment
end preOpenStack


command saveMyCardToDataStack
    copy card "Cardone" of this stack to stack tDataStackPath
   copy card "Cardtwo" of this stack to stack tDataStackPath
     delete the first card of stack tDataStackPath
   
end saveMyCardToDataStack

command restoreMyCardFromDataStack
   copy card "Cardone" of stack tDataStackPath to this stack
   copy card "Cardtwo" of stack tDataStackPath to this stack
     delete the first card of this stack

end restoreMyCardFromDataStack

on shutdown
saveMyCardToDataStack
 save stack tDataStackPath 
end shutdown

On openStack
   go card "CardOne"
end openStack
Thanks in advance for your answers.
Boris
My application "GeoMaths" is available on Itunes and Google Play !

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Save data or custom properties

Post by jmburnod » Sat Feb 01, 2014 11:27 am

Hi Boris,
I think simulator can't work with external files.
Does someone can confirm it (I didn't found where I read this point) ?
Best
Jean-Marc
https://alternatic.ch

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Sat Feb 01, 2014 11:32 am

Hi Jean-Marc,
i have tested on my Ipad with IOS 7 it's the same problem.
Best regards
Boris
My application "GeoMaths" is available on Itunes and Google Play !

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Save data or custom properties

Post by jmburnod » Sat Feb 01, 2014 11:51 am

It works on OSX (ide and standalone) but not with simulator or standalone IOS ?
https://alternatic.ch

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Sat Feb 01, 2014 12:22 pm

It works on standalone OSX, but not on simulator IOS and on Ipad.
My application "GeoMaths" is available on Itunes and Google Play !

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Save data or custom properties

Post by jmburnod » Sat Feb 01, 2014 12:39 pm

HI Boris,
Sorry, no idea.
I never used stacks to save data, using files works fine for what I do

Best regards
Jean-Marc
https://alternatic.ch

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Sat Feb 01, 2014 1:05 pm

Hi Jean-Marc,
thank you for searching a solution.
For saving the custom properties in files, have you a example for testing and understand how it's works ?
Best regards
Boris
My application "GeoMaths" is available on Itunes and Google Play !

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Save data or custom properties

Post by jmburnod » Sat Feb 01, 2014 1:47 pm

Hi Boris,
Depend what you store in CustomProp, but something like that

Code: Select all

on preOpenStack
   put specialFolderPath("documents") & "/" & "_" & "MyPrefs" into tDataPrefPath
   if not (there is a file tDataPrefPath) then 
      get the uMyPrefs of this stack
      put it into url("File:" & tDataPrefPath)
   else
      set the uMyPrefs of this stack to url("File:" & tDataPrefPath)
   end if
end preOpenStack
All the best
Jean-Marc
https://alternatic.ch

sirobchGA0cda
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 47
Joined: Wed Dec 21, 2011 10:42 am
Contact:

Re: Save data or custom properties

Post by sirobchGA0cda » Sat Feb 01, 2014 2:11 pm

Hi Jean-Marc,
thanks for your code.
If it's possible to help me with the stack problem, i will understanding why i have this problem ?
Best regards
Boris
My application "GeoMaths" is available on Itunes and Google Play !

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

Re: Save data or custom properties

Post by jacque » Sun Feb 02, 2014 1:25 am

Moving cards can be a problem if you don't keep careful track of where they are. Whenever possible it's almost always better to move the data rather than copy an entire card.

When the data stack is first created, it will contain a single blank card. Then the two cards from the mainstack are copied to it, and the blank one is deleted. Now you have a 2-card stack with your data on each card. That part is okay.

Immediately after creating the new stack, the restoreMyCardFromDataStack handler runs. It probably shouldn't if this is a first run. It copies the two cards back to the mainstack and deletes the first card of the mainstack. Now the mainstack contains cardTwo (original), cardOne (copied) and cardTwo (original.) It's grown by one card.

When the app closes, it runs saveMyCardToDataStack. That copies two cards back to the data stack but deletes only the first one. Now the data stack contains cardTwo (original), cardOne (copied), and card Two (copied). Every time the handler runs, the stack will increase by one card. The next time the app starts up it will retrieve the copied cardOne and the original cardTwo, because those are the first ones it will find. It will never find the copied cardTwo because that one keeps getting shoved to the back of the stack.

Since standalones can't save themselves to disk, every launch will restore the standalone to the condition it had when it was compiled, with only a single instance of each card. However, unless you delete the standalone entirely from the device, the data stack will remain and continue to increase in size. When your app tries to copy a card from it, you may or may not get the one you expect. It could be the original card (without any changes, copied initially from the app) or it might be one with changes that you made during that session.

There are ways to fix that. You'll need to make sure you are deleting enough cards, and deleting them from the right stack ("this stack" isn't always what you think it is.) But a much better way to handle things is to just save the data itself without bringing a whole card along with it. So if you can let us know what type of data you need to save, we can give you some good ideas about how to do that. Usually either custom properties or text files are used.

That said, if you are trying to save whole card layouts and all the objects that are on the card, then yeah -- that's one case where it's actually easier to move cards around than to save each component individually. But if you just need to save some text (like game scores, or user data) or just the locations of existing objects, then files or properties are the way to go.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply