PropertyBag - HowTo Use it
It is a good thing that a lot of users are using PropertyBag and it is helping them. However because a lot of users are NEW to Corona and Lua and from the looks of things Programming, I thought rather than respond to every question with the same reply, I'd rather put up a post here that everyone can refer to.
So,
How does PropertyBag work?
1. You need to include propertyBag in your projects
2. You need to create an instance that you will use in your code
3. You need to create the property/properties you need to work with
What this has done is create a property in memory, which means the moment you quit the app, this is not retained, it will be destroyed, so to make it persistent and retain the data, we need to save it to the device. for which we need to use the function
So keep in mind that everytime you change a property and want it to be saved, you need to call the function saveToFile(), if you do not do that, the data you just set will not be saved/available the next time you use the app.
4. When you call init, it also tries to load the values if a previous version of property was saved on disk. So if you had called saveToFile(), all the data that was saved will automatically be loaded the next time the app is run.
However, there could be instances where you might want to refresh the data/re-load the data from the disk, you would use prop:GetFromDisk() to do that.
Now the most common mistake than many beginners make is
let's see what this does,
the first line sets the value for the property
the second line asks the propertyBag to refresh, i.e. reload all the data that was on the disk, which means overwrite the data we just set, in some ways more like an undo.
the third line then saves the refreshed data back to disk.
so the net result of this operation will be that the values that you wanted to change have not changed, and you are back with the same values, as if you never saved them.
In simple terms, let us look at it as
1. Write something on a piece of paper
2. Erase everything to bring it back to the state it was
3. Make a photocopy
now wonder why the photocopy does not have what you wrote on it.
So to do that, you might have to interchange the step #2 and #3
Hope that explains the situation and how to use it.
Another issue that many have faced is when they try to compare property values.
A large number of users have tried code like
1. This is a very bad way of doing things, this is not only Lazy programming, but also will in majority of the cases cause grief.
so how do you use this?
now even if you were to use this, there can be issues, this arises from the Lua function file:read, the return value of this is a string, because it returns a string and what many try to compare is a number the results are unlikely to be what you expect them to be.
So,
1. We need to convert both to be the same, some users have tried to convert the comparing value into string, which is again wrong as strings compare differently than numbers.
So we will convert that into numbers.
If there are any other question, write to me on dev[at]oz-apps[dot]com and I could write further articles or add to this one with more examples on how to use PropertyBag.
Keep reading, I will soon add how to save data to an alternative file than just defaults.
cheers,
?:)
So,
How does PropertyBag work?
1. You need to include propertyBag in your projects
local propertyBag = require("property")
2. You need to create an instance that you will use in your code
local prop = propertyBag:init()
3. You need to create the property/properties you need to work with
prop:setProperty("MyName","Jayant C Varma") prop:setProperty("myScore",15000)
What this has done is create a property in memory, which means the moment you quit the app, this is not retained, it will be destroyed, so to make it persistent and retain the data, we need to save it to the device. for which we need to use the function
prop:saveToFile()
So keep in mind that everytime you change a property and want it to be saved, you need to call the function saveToFile(), if you do not do that, the data you just set will not be saved/available the next time you use the app.
4. When you call init, it also tries to load the values if a previous version of property was saved on disk. So if you had called saveToFile(), all the data that was saved will automatically be loaded the next time the app is run.
However, there could be instances where you might want to refresh the data/re-load the data from the disk, you would use prop:GetFromDisk() to do that.
Now the most common mistake than many beginners make is
prop:setProperty("NewValue",100) prop:getFromDisk() prop:saveToFile()
let's see what this does,
the first line sets the value for the property
the second line asks the propertyBag to refresh, i.e. reload all the data that was on the disk, which means overwrite the data we just set, in some ways more like an undo.
the third line then saves the refreshed data back to disk.
so the net result of this operation will be that the values that you wanted to change have not changed, and you are back with the same values, as if you never saved them.
In simple terms, let us look at it as
1. Write something on a piece of paper
2. Erase everything to bring it back to the state it was
3. Make a photocopy
now wonder why the photocopy does not have what you wrote on it.
So to do that, you might have to interchange the step #2 and #3
prop:setProperty("NewValue",100) prop:saveToFile() prop:getFromDisk()
Hope that explains the situation and how to use it.
Another issue that many have faced is when they try to compare property values.
A large number of users have tried code like
if prop:getProperty("score") > 100 then -- do something here end
1. This is a very bad way of doing things, this is not only Lazy programming, but also will in majority of the cases cause grief.
so how do you use this?
local result = prop:getProperty("score") if result > 100 then --do something end
now even if you were to use this, there can be issues, this arises from the Lua function file:read, the return value of this is a string, because it returns a string and what many try to compare is a number the results are unlikely to be what you expect them to be.
So,
1. We need to convert both to be the same, some users have tried to convert the comparing value into string, which is again wrong as strings compare differently than numbers.
So we will convert that into numbers.
local result = prop:getProperty("score") result = tonumber(result) if result > 100 then --do something end
If there are any other question, write to me on dev[at]oz-apps[dot]com and I could write further articles or add to this one with more examples on how to use PropertyBag.
Keep reading, I will soon add how to save data to an alternative file than just defaults.
cheers,
?:)
Comments
Post a Comment