Page 1 of 1
CustomProperties some clarity please
Posted: Wed Mar 06, 2013 11:19 am
by Traxgeek
So, Custom Properties :
Am I right/wrong in believing that :
-1- We can use CustomProperties (CPs) in lieu of Global Variables and, in fact, it is BETTER to use CPs than Globals (in which case WHY please ?)
-2- Whilst CPs may be changed at run time their 'state' when closing a MOBILE app will NOT be saved (just like a Global) because of write access rules to the application folder prohibiting data writes ?
-3- CP's WILL be saved on closing DESKTOP apps (Mac, Windows, Linux) ?
-4- If CPs are to be used for application preference / persistent data (language choice, date formats etc) then these CPs will need to be 'written out' to a preferences file (or somesuch) before app. close/exit and 'read in' on app. startup ? (If so, is this just a simple file create/write/read operation ?)
A little basic I'm sure but...
Thanks
Re: CustomProperties some clarity please
Posted: Wed Mar 06, 2013 5:13 pm
by magice
The main reason for not using globals is because it is easy to accidentally reuse the the same variable name later and that can be very difficult to diagnose. It can also make reading the code harder if you don't know the code in it's entirety. I actually break the rule, and make one global array for larger apps named tMainArray. I then can save local variables to elements of the global array when I need a global variable. The only drawback to this is that I need to make a "cheat sheet" so I can remember which element is which variable. The benefit of keeping them in one array for me outweighs that drawback. I can then simply write the array to a text file and I have effectively saved the stack. Then on openstack if their is a text file I have it read the file and write the elements back to the appropriate variables and fields. Of course I'm not much more than a newbie myself, so if some of the more experienced users make a better suggestion, listen to them. Using custom properties instead of globals prevents the accidentally duplicated name, but to me is just as hard to follow when you come back a year later so I still need the "cheat sheet". They are great for things like variables that determine the state of a button that toggles, because it can be a custom property of that same button.
Also, custom properties are not saved in desktop apps either (in the standalone version). The only way to do that is to have a standalone launch a separate un-compiled .rev file.
Re: CustomProperties some clarity please
Posted: Wed Mar 06, 2013 5:17 pm
by Klaus
Hi Trax,
1. there is no BETTER in this case, although many developers like me prefer CPs over GLOBALs.
Maybe because they:
a. do not need to be declared and
b. they will (CAN) be saved with the stack
2. and 3. NO standalone can save itself, NEVER!
4. Yes, just a simple file operation! Example:
Code: Select all
on closestack
put specialfolderpath("documents") & "/MyAppFolder/the_prefs.txt" into tPrefsFile
put the cWhatEverCustomProp of this stack into url("file:" & tPrefsFile)
...
end closestack
on preopenstack
put specialfolderpath("documents") & "/MyAppFolder/the_prefs.txt" into tPrefsFile
## App has been opened before:
if there is a file tPrefsFile then
set the cWhatEverCustomProp of this stack to url("file:" & tPrefsFile)
end if
...
end preopenstack
Best
Klaus
Re: CustomProperties some clarity please
Posted: Wed Mar 06, 2013 7:14 pm
by Traxgeek
Magice & Klaus
Thanks for clearing this up for me... I'm definitely a newbie to LC but beginning to REALLY like it ! Hardest thing for me is remembering NOT to apply C, VB, .NET principles and syntax. I have yet to deploy a LC app to anything BUT I'm getting there. Another couple of weeks and I'm hopeful to try and deploy across Mac, Windows, Android and iOS... then the fun will start !
Again, thanks both. Appreciated.
Regards.
Re: CustomProperties some clarity please
Posted: Wed Mar 06, 2013 10:57 pm
by FourthWorld
Traxgeek wrote:-1- We can use CustomProperties (CPs) in lieu of Global Variables and, in fact, it is BETTER to use CPs than Globals (in which case WHY please ?)
There are many opinions about global variables, but most programming languages include them for a reason. There's a useful role for both custom properties and global variables:
Custom properties can be useful when you need persistence, or when the data is related to the object their bound to.
Global variables can be a good choice for data you don't want to survive beyond the session, or when performance is critical.
-2- Whilst CPs may be changed at run time their 'state' when closing a MOBILE app will NOT be saved (just like a Global) because of write access rules to the application folder prohibiting data writes ?
-3- CP's WILL be saved on closing DESKTOP apps (Mac, Windows, Linux) ?
No application can modify its state on disk, per OS restrictions.
To save objects with custom properties, use a separate stack file from the one you build the standalone with.
See the Dictionary entries for the create and clone commands for how to create a stack file.
Personally, I love using stack files for data storage. So convenient, so efficient.
For details on saving data from a standalone, see:
http://livecodejournal.com/tutorials/sa ... ution.html
-4- If CPs are to be used for application preference / persistent data (language choice, date formats etc) then these CPs will need to be 'written out' to a preferences file (or somesuch) before app. close/exit and 'read in' on app. startup ? (If so, is this just a simple file create/write/read operation ?)
The save command will save any stack that is not in a read-only state (that is, not the standalone, doesn't have its read-only bit set in the OS, and doesn't have its cantModify property set to true).
You may also want to look at the specialFolderPath function in the dictionary, and these supplemental notes on using it:
http://www.sonsothunder.com/devres/live ... ile010.htm
Re: CustomProperties some clarity please
Posted: Fri Mar 08, 2013 10:06 am
by Traxgeek
Hey, Thanks Richard.
The stack method sounds right up my street for my particular purpose of option persistence between sessions.
I'm just firing up the tutorial.
Thanks again !