Page 1 of 1

Custom properties

Posted: Tue Jan 27, 2015 8:38 am
by Opaquer
Hey everyone

So, I've been working on an app (and it's going well, yay!)

That said, I've just run into a slight issue. I found out that iPads aren't great with keeping the app running in the background, so if someone presses the home button, the app loses all the data :(. Now, I was doing some reading and came across custom properties, which look perfect for the job! Unfortunately, I can't wrap my head around it no matter how much I try. Since my app is a standalone application, I can't save the data into the stack, right? So, do I need to make a new substack, and do save stack (or whatever the command is) for that stack? Or do I need to use the "set the customProperty of stack to" thing? What's the difference between save stack and set the customProperty functions?

Any help would be appreciated :)

Thanks in advanced!

Re: Custom properties

Posted: Tue Jan 27, 2015 9:06 am
by SparkOut
To save the state of your app between runs you will need to save the required data in some way, in the specialFolderPath("documents") folder. You can save the data in whatever suitable format you choose. For a few fields a plain text file may be all you need. However, a separate stack (not a substack) can be very handy to use as it will keep a full representation of all the data and properties you set on it without having to extract or convert data into a text format.
A custom property is not saved anywhere separately, it is part of the stack. Jacques made a very good explanation here http://forums.livecode.com/viewtopic.ph ... 81#p117281

Re: Custom properties

Posted: Tue Jan 27, 2015 9:30 am
by Opaquer
SparkOut wrote:To save the state of your app between runs you will need to save the required data in some way, in the specialFolderPath("documents") folder. You can save the data in whatever suitable format you choose. For a few fields a plain text file may be all you need. However, a separate stack (not a substack) can be very handy to use as it will keep a full representation of all the data and properties you set on it without having to extract or convert data into a text format.
A custom property is not saved anywhere separately, it is part of the stack. Jacques made a very good explanation here http://forums.livecode.com/viewtopic.ph ... 81#p117281
Thanks SparkOut, I think I'm slowly getting the hang of it. Quick question - how do I make this new stack for it? I've only ever used one stack per application, so not sure if there's anything special that needs to be done :P. Also, I want to be able to reset the custom properties if a certain button is pressed - how would I go about doing that? And how do I go about reading the custom properties? The saved data will be in the form of an array, so that should be pretty easy to get across I would imagine?

Re: Custom properties

Posted: Tue Jan 27, 2015 10:20 am
by SparkOut
Try this lesson, it should give you the info you need.
http://lessons.runrev.com/s/lessons/m/4 ... pplication

Re: Custom properties

Posted: Tue Jan 27, 2015 1:19 pm
by keram
SparkOut wrote: However, a separate stack (not a substack) can be very handy to use....
Why not a substack?
What's the disadvantage of using it for custom properties?

Re: Custom properties

Posted: Tue Jan 27, 2015 1:45 pm
by Klaus
keram wrote:
SparkOut wrote: However, a separate stack (not a substack) can be very handy to use....
Why not a substack?
What's the disadvantage of using it for custom properties?
Well, a substack is part of the stackfile = standalone and cannot be saved 8)

Re: Custom properties

Posted: Wed Jan 28, 2015 12:18 am
by Opaquer
SparkOut wrote:Try this lesson, it should give you the info you need.
http://lessons.runrev.com/s/lessons/m/4 ... pplication
Thanks again SparkOut! So, can I create a new stack and save an array from the current stack I have as the custom property of the new stack, then save stack when it's closed? Is that right? Also how do I delete the custom property? So I'd have this code in my current stack with all my other code:

set the cSave of stack "SaveStack" to arrayrecord

Then I'd have the save stack on closeStack, which is easy enough. Then to delete the custom property, would I use set the cSave of stack "SaveStack" to ""? Finally since I want to be able to open the stack and keep the data there (if there's any data), in preOpenStack, would I have something like:

if the cSave of stack "SaveStack" is not "" then
-- do the stuff I need to do
end if

Or am I missing something? Thanks again for the help! I think I might be getting the idea of custom properties finally :P!

Re: Custom properties

Posted: Wed Jan 28, 2015 1:16 am
by Simon
Hi Opaquer,
I think I might be getting the idea of custom properties finally
I just want to make sure you have this right.
What that lesson teaches you is how to use a "Splash Stack" (called launcher there) to save data, while it does use custom properties, it's just one use of them.

You can replace all of your global variables in an app with
set the cIsItCool of this stack to true
and
get the cIsItCool of this stack
Yes they will disappear when the stack is closed but it acts just like a variable.

Imagine your MainStack has a field full of names and addresses, if you add a new address and saved the stack it will hold that new address. A custom property wouldn't be needed.

You will get into interesting territory if you intend to use this with iOS, if you are going to use this method say so now. You may want to consider just using a text file.

Simon

Re: Custom properties

Posted: Wed Jan 28, 2015 1:35 am
by Opaquer
Simon wrote:Hi Opaquer,
I think I might be getting the idea of custom properties finally
I just want to make sure you have this right.
What that lesson teaches you is how to use a "Splash Stack" (called launcher there) to save data, while it does use custom properties, it's just one use of them.

You can replace all of your global variables in an app with
set the cIsItCool of this stack to true
and
get the cIsItCool of this stack
Yes they will disappear when the stack is closed but it acts just like a variable.

Imagine your MainStack has a field full of names and addresses, if you add a new address and saved the stack it will hold that new address. A custom property wouldn't be needed.

You will get into interesting territory if you intend to use this with iOS, if you are going to use this method say so now. You may want to consider just using a text file.

Simon
Hey Simon

I am planning on using this with iOS. Do you recommend using a different system instead of custom properties?

Re: Custom properties

Posted: Wed Jan 28, 2015 1:59 am
by Simon
Ok here is the deal, just a little explanation.
For iOS you are not allowed to download binaries because they could have code in them that could be used as a virus.
I use the Splash stack method in my desktop standalones so that I can update the main stack via a download.
Now here is where it gets weird...
I cannot use that method for iOS because it's against the rules but, your main stack will appear to be the same thing. Both would be located in specialFolderPath ("documents")/mainStack.livecode. Now it's clear what you are using it for is not against the Apple rules to me, and if you were to go through with it I'd really like to know if your app gets accepted, lots of people will.

Just want to make sure you have a backup plan.

Code: Select all

put arrayEncode(myArray) into URL("binfile:" & specialFolderPath ("documents")&"/myText.txt")
(I forget if that is "file:" or "binfile:")

Code: Select all

put URL("binfile:" & specialFolderPath ("documents")&"/myText.txt") into myArray
arrayDecode(myArray)
There is probably a one liner for that one.
I'm just typing those from memory so I might have something wrong but it is pretty much that easy. There will be some checking added to see if the file is there or not the for the first time the app is run.

Well to set your mind at some ease a database is a binary file and they allow downloading of them.

Simon

Re: Custom properties

Posted: Wed Jan 28, 2015 4:19 am
by Opaquer
Hi Simon

I'm not going to be putting this on the app store, so I can't quite help with finding out if it'll be accepted :(. Also, I'm liking your backup plan. It's looking very handy! Quick question though - is there any way for my users to access the text file?

So all I'd have to do is check if the file exists, and if so, use the arrayDecode on it, but if not, go as normal. Then whenever my array gets saved, just to use the arrayEncode one to save it as a text file in the iPad itself. Finally, when the delete button is pressed, delete the text file? Could it really be that simple?

Re: Custom properties

Posted: Wed Jan 28, 2015 4:38 am
by Simon
Hi Opaquer,
is there any way for my users to access the text file?
I'm pretty sure not but I'm not up on all the jailbreak stuff.
If they did they'd still have to know how to use arrayDecode or decipher a "Serializes a LiveCode array."
Finally, when the delete button is pressed, delete the text file?
Yeppers to that and the rest.
Could it really be that simple?
OH YES! :)

Note that all that encoding has to do with using an array, but you can just put a variable or a bunch of variables or a list of things into a text file and save that (all code the same minus the encoding). Don't forget to check that binfile vs file.

Now to be fair to using a stack to hold things... using a stack is Great and I don't want to put you off learning about it because they are really cool at storing stuff like binary data images n' stuff.

Tip: look up and read carefully specialFolderPath because someday soon you will want to Copy a file along with your stack and it will end up in the "engine" folder and you'll want to move it to the "documents" folder if you want to edit it. (no editing in the "engine")

Simon

Re: Custom properties

Posted: Wed Jan 28, 2015 4:42 am
by Opaquer
Simon wrote:Hi Opaquer,
is there any way for my users to access the text file?
I'm pretty sure not but I'm not up on all the jailbreak stuff.
If they did they'd still have to know how to use arrayDecode or decipher a "Serializes a LiveCode array."
Finally, when the delete button is pressed, delete the text file?
Yeppers to that and the rest.
Could it really be that simple?
OH YES! :)

Note that all that encoding has to do with using an array, but you can just put a variable or a bunch of variables or a list of things into a text file and save that (all code the same minus the encoding). Don't forget to check that binfile vs file.

Now to be fair to using a stack to hold things... using a stack is Great and I don't want to put you off learning about it because they are really cool at storing stuff like binary data images n' stuff.

Tip: look up and read carefully specialFolderPath because someday soon you will want to Copy a file along with your stack and it will end up in the "engine" folder and you'll want to move it to the "documents" folder if you want to edit it. (no editing in the "engine")

Simon
Thanks so much Simon! This is so much help! I'll start working on it (after lunch! I'm starving!) and hopefully shouldn't have any other issues, so if all goes well, it should be ready for beta testing by work related people later today, which is awesome :D! Again, thanks so much!

EDIT: Also I forgot to mention - I've got one variable that I want to store between uses, and it's already in the form of an array, so this is perfect!

Double edit: Thanks again Simon, I managed to get it working! Huzzah!