Saving Data from a Background Process (setProperty)

Jim (or others who have done this)...

My background process is now working really well. Thanks for the help. I do wish in a Data Field, that (like watch faces and widgets) the 5 min trigger delay would be reset upon the start of a new activity or device power cycle. I don't get why a data field would act differently. The whole point of the 5 minutes is to not overwhelm the device. So this one time reset seems like a good idea.

But, since this isn't available. I need to save the prior data along with a timestamp. Then retrieve that old data (if not too old) while waiting for the 5 min trigger delay to expire.

I could save this data using a setProperty call in the background process that gets the data. Or in the foreground process each time it gets new data.

Any tips on a Best Practice? My idea is to first check for new data. If waiting for the delay, then go look in the saved properties and use that value if its timestamp is < 30 mins.

Thanks!
  • I posted about how if you delete the terminal event when your DF exits' it will run right away the next time you register temporal events (as long as it's more than 5 minutes). That's probably the best approach. You do this in onStop() but only when it's called by the DF and not the background.

    This way the background doesn't keep running after your DF exits.

    You can't save to the ObjectStore/Application.Storage in the background, but as you said, what you can do, is save the data in onBackgroundData() in the main app. That way when the DF starts, you can just use that to start. You probably want to include a timestamp, so after a certain amount of time you discard what was saved and don't use it.
  • Hi Jim - for me, I register my TemporalEvent using a MOMENT not a DURATION in getInitialView(). Using a MOMENT, it *seems* that there is no "next event" automatically scheduled when I check "Background.getTemporalEventRegisteredTime()". So in the "onBackgroundData(data)" method, I check for last time it was run and register the next one 5 mins from last time, or NOW if lastime is NULL.

    What I'm trying to do now is have a somewhat reeasonable value if my device resets... rather than have no value for a few minutes, it'll use the last good value if recent. Thanks again Jim. I'll add the setProperty() to the onBackgroundData() method.
  • Are you waiting 5 minutes or more between runs? Even with a moment, it's got to be 5 minutes or more after the last time it ran.
  • Are you waiting 5 minutes or more between runs? Even with a moment, it's got to be 5 minutes or more after the last time it ran.


    Yes I am.

    What confused me initially was when you mentioned that I have to kill the background processes in the onStop() method or it'll just keep running 24/7. But since I register it using a MOMENT, it doesn't seem to run more than once unless I re-register it again each time in onBackgroundData().

    I got the setProperty() working now. It is awesome. Thank you!!
  • Yes, it you use a moment, it will only run once - that moment or as soon after that it can. You need to re-register to get it to fire again
  • I got the setProperty() working now. It is awesome. Thank you!!

    How id you get it working?

    I get

    "Exception: Background processes cannot modify the object store"

    which means that I can't save any of the parameters in my app which was working before I added background to do TemporalEvents.

  • because you try use STORAGE and you should use PROPERTIES

  • Only some devices allow doing this in a background service.  See https://developer.garmin.com/connect-iq/api-docs/Toybox/Application/Properties.html

    The device must have CIQ 3.2.0 or greater or an exception is thrown.   You may also need to use Property.setValue() or Storage.setValue()

  • Thanks, I am using

    Application.getApp().setProperty("lastEventTime",eventTime);

    CIQ >4.0 on a Venu 2 Plus

    Is this incompatible?

  • Which SDK are you using?