I am having trouble figuring out how to save variable values. For example, on one page I have a variable that the user can set with the use of a + button or - button. Value starts at 5 but they can move it up or down. Obviously, when they leave the page or app and come back, the value gets reset to 5. But i need that variable to stay at whatever value they set it at whether they leave the app or the page and come back.
I’ve tried searching the forums and watching the tutorials on variables and data and every time I think I have it, it doesn’t save the data. I’m hoping someone can explain it a little more clearly to me. Thanks.
Don’t app variables reset when you restart the app? I need the numbers to persist through restarts until the user presses a button and chooses to reset the values to default.
I’ve gone through the tutorials, as I said above, and I’m having trouble figuring out how to save data.
I have tried both methods and have been unable to make it work. When I leave the page and come back, it always has the variables set back to default values. Do the Local Storage and Data Variable options work in the SAP Appgyver Preview app?
I tried to use the Set Item to Storage and Get Item from Storage functions, but couldn’t get it to work. I did something like:
When Var1 changes → Set Item to Storage [name]
and
When page is Mounted → Get Item from Storage [name] → Set Var1 to [value].
I’m trying it again and I run into a problem trying to update the page variable after getting the item from storage. So I’m doing,
Get Item from Storage [name] → Set page Variable
The variable that I saved is a number, but under the options for Set Page Variable I try to bind the value to something, and I thought it would be Output of Another Node (get item from storage), but the only choices are Item and Item Found, both which are marked as incompatible.
How do I get the number value from storage and apply it to the variable?
First, I went to Data tab and I created an On-Device Storage object called Settings that contained all the values I wanted to save for my settings. You can set the type of each of these values to be correct for the type of variable you later want to set it to (e.g. text, number, true/false).
Then, in my Global Canvas logic, I retrieve the previously saved settings on app launch:
(The extra Save Default Settings & Get Record blocks are to handle creating the settings record with good default values on the user’s very first launch.)
I use a standard Get Record block to retrieve all of my saved settings. Then I set each of my app variables that hold the settings by extracting the specific setting from my settings object. For example:
Then, to automatically update the settings record when any of the settings are changed while using the app, I do this also in the global canvas (one “changed” event for each of my settings variables):
You could probably also use an app variable that is an object containing all your settings values and it would require fewer blocks to extract each setting from the record and you would only have to handle one “changed” event. But using separate variables is the way I approached it and I know it works.