Good new way to save data and interface
Posted: Thu Nov 07, 2013 3:29 pm
Hi there,
I set about creating an app some time ago that allowed a user to add interface elements as they used the app.
So for example they could click "New Button" and the app would create a new group of objects and place them in the interface for the user to interact with. Each object has data associated with it.
My issue was finding a way to save the user created interface when the app closed and restoring it when the app re-opened.
I looked at various methods found here in the forum but none seemed appropriate for an iOS app long term.
It looked like i was going to have to do things the hard way and iterate over every element on save storing every piece of data about each element (and there is a lot) to text or database file.
Today i found a solution that works for me, and thought i would share it as i have not seen the like spoke about anywhere before.
Step 1) I have used this method before to store custom properties in a separate stack on save and re-instate them on start-up.
Now we have created a new stack at the location : tDataStackPath
Called : myDataStack
In the past i have stored data like custom properties to this stack like this...
Include the global variable at the top of any object, card or stack you are going to refer to the data stack
Now setting a custom property of the data stack is like this :
You can save the data stack now or in the on shutdown handler like :
You can obviously get that data back from the data stack at any time like :
But heres the thing...
Instead of me iterating over possibly thousands of elements and data i want to save at shutdown (headache).
I can use this method now :
step 2)
I save all my custom properties right into the object they belong to in the main stack (intuitive).
Now on shutdown i do this :
And remember we are restoring the card from the data stack in the preOpenCard handler above.
This means that the card in its entirety, all objects scripts and custom properties of the objects are saved and restored properly with quite minimal effort, it's quite seamless.
Hopefully i don't find issue with this method, because i love love love it
I set about creating an app some time ago that allowed a user to add interface elements as they used the app.
So for example they could click "New Button" and the app would create a new group of objects and place them in the interface for the user to interact with. Each object has data associated with it.
My issue was finding a way to save the user created interface when the app closed and restoring it when the app re-opened.
I looked at various methods found here in the forum but none seemed appropriate for an iOS app long term.
It looked like i was going to have to do things the hard way and iterate over every element on save storing every piece of data about each element (and there is a lot) to text or database file.
Today i found a solution that works for me, and thought i would share it as i have not seen the like spoke about anywhere before.
Step 1) I have used this method before to store custom properties in a separate stack on save and re-instate them on start-up.
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"
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
Called : myDataStack
In the past i have stored data like custom properties to this stack like this...
Include the global variable at the top of any object, card or stack you are going to refer to the data stack
Code: Select all
global tDataStackPath
Code: Select all
set the username of stack tDataStackPath to "Happy Harry"
Code: Select all
save stack tDataStackPath
Code: Select all
put the username of stack tDataStackPath into tUserName
Instead of me iterating over possibly thousands of elements and data i want to save at shutdown (headache).
I can use this method now :
step 2)
I save all my custom properties right into the object they belong to in the main stack (intuitive).
Now on shutdown i do this :
Code: Select all
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
This means that the card in its entirety, all objects scripts and custom properties of the objects are saved and restored properly with quite minimal effort, it's quite seamless.
Hopefully i don't find issue with this method, because i love love love it
