Where/when to use Application.storage getvalue/setvalue

A bit of a newbie question, but is there a "correct" way to use the Application.storage values???

One could either add code to the a initialize functions and copy the values to a (global) var. Or just call the .getvalue in the onUpdate function. Either when needed or copy the data to a local var at first.  

I have data that only updates once a day and is needed both in the glance and main view onUpdate. As well as a few settings that's updated view the menu and used throughout the onUpdate.  

Are there any speed or memory considerations to be aware of. Or is either way just fine?

Thanks. 

  • In general, you only want to make calls that hit the file system as infrequently as possible as those calls are expensive.  In the case of Application.Storage reading something once at startup and then keeping the result in memory is more efficient battery wise than re-reading Storage all the time in onUpdate().  It's not much but every little bit helps. If you do a setValue(), you have the value in your app, so no need to do a getValue() for that key.

    Also, you want to consider how you use a key.  Let's say you have 8 color values you want to save in Storage.  You could do that with 8 keys (8 file system hits when reading them) or you could save them as an array of the 8 colors (1 file system hit to read all 8)

    I'm not sure why you are coping stuff between a global and local var.  If it's in a global, you can access it.  If it's in a class variable, you can access it from another class by passing the class instance of the first to the second class.  If it's local to a function, move it to the class level.  For example, lets say I have something in the view, but you want to access it from a delegate.  You pass the view as a parameter to the delegate.

  • I'm not copying from global to local or vice versa. Was trying to ask if one should getvalue->local on every onUpdate or getvalue->global and use that trough out. I think you answered that.

    But where is the best place to do the getvalue? In the glance init or onUpdate? But you can't always be sure the glance is viewed, can you? So should one also do it in the init/onUpdate for the main view or is there a place more suited for this? Something that will always be run, not matter how the widget is launched

    Thanks.

  • Something that will always be run, not matter how the widget is launched

    How about your app class's onStart() function?

  • In my watchface, I have a global class, which maintains application settings and synchronizes them to persistent storage. The settings are read (using Storage.getValue()) in the constructor of that class. Whenever a setting changes, it is stored using Storage.setValue().

  • Be careful with onStart() if you have a background service, as it runs each time the background runs.  The background has very limited memory and you don't want to use a bunch of memory to load stuff from Storage you don't need or want.