How to Save Variable values

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.

An app variable persists, a page variable doesn´t.

If you haven´t already gone through all the tutorials they really are worthwhile, I think they cover this.

See this power-up video

You can store it in Local Storage or in a Database.

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.

Oh sorry, I misunderstood. You, they reset every restart.

You need to link to a back end database then. Lots of options, but probably the most is written about Firebase (making it the easiest to implement).

Good luck.

Or save it in local storage. That’s easy.

@jaymer_jaymer

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].

Am I missing something?

Seems correct. When you get the Storage Item out it in an app variable to use it …

Works perfectly in Preview.

Thanks for your help.

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:

SetAppVar

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):

UpdateSaved

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.