watch face : how to save data and run in background

I'm programming my own whatchface, but I am not a programmer, so i am struggling. I am doing this to 
I managed to get my watch face work. Basically I draw a number of dots depending on how many steps you do in a minute. the more dots the more steps.
Everything works up to the moment you change screen.

I tried to search for someone who already did something similar, but i am not getting it, it's been a while, coding is new.

Summarizing i need to do this steps

1) I loose all data, meaning the steps done in the previous minutes. So I need some idea on how to save data when i change screen. I'm using App.Storage.setValue("myStepsN", step60_n); and  step60_n = App.Storage.getValue("myStepsN"); I tried to set them in        function onStart(state) {  and  function onStop(state) {    but with no success where step_ is an array [step_min1 step_min2 ... step_min60]

2) I would like to store / compute the step done each minute in the same array [min1 min2 ... min60] i use in the main script also when the watch face is not visualized. I guess i can copy paste the same code somewhere else to do it. I guess a background process, then again save them each minute to be able to reload them when going back to the watchface.

3) when getting back to the watchface load back the data to be able to print them again, the one computed when the watchface was active + when it was off. Now all the data are lost, since of course i did not get to save them. So 1) and 3) are complementary points.

Somebody can provide help? As simple as possible, I am not so good.

Any help will be appreciated

  • The basics are you want to save the array to Storage when the WF exits and load the array from Storage when you start, (onStop and onStart could be used), but there's more.

    Let's say the user uses the run activity and uses it for an hour - everything in your array is "stale".  You need a way to detect that with a way to timestamp things.

    You could fill in some of the blanks with a background service, but they run at most every 5 minutes, so a valid reading, then 4 misses, a valid reading, then 4 misses...  That also complicates using onStop, as that runs for the background service as well as the watchface itself.

    And you also have the case where your watch face isn't the active one, so it's background service won't run during that time.  The background service also doesn't run if you are running a CIQ device app.

  • okay, one step at the time; I get to load and save data, BUT only if they are in the same script. i.e. DotsView.mc and DotsApp.mc, NOT between them, I don't know why.

    I tried to put App.Storage.setValue("myStepsN", step60_n); and  step60_n = App.Storage.getValue("myStepsN"); basically everywhere, Still everything seems not to work.
    Is it too much if I ask you an example or specific direction wher to put those 2 lines?

    Since you tell me in background data are saved every 5 min, i will deal with it next. I tried as well forcing on hide on the simulator. 

    I put some comment line to see what part of the code are called each times, still I can't figure it out how to simulate the change screen / background properly. 
    Each time i download the script to the watch and try it.

  • When it comes to setValue and getValue, you could be running into an issue with how/where step60_n is defined.

    The easiest way to handle this is to declare it as a global variable (outside of any class definitions) and have that the only place the variable is defined.