Restarting background temporal process after settings change.

My watch face connects to a web server to request data. To save on calls and battery, it does it every hour.
I have it set to use GPS for the last activity, to determine location. Also, user can change manually the location in settings.
Problem is that if the settings are changed (or location changed in general), nothing happens until the next interval event comes, up to one hour.

I request the update on onSettingsChanged(), but it does not trigger the background process. If it does, I risk to run into the error of having it run prior to the minimum 5min interval.

Is there any workaround?
I tried the guide's sample using getLastTemporalEventTime(), but it does not seem to work.
  • when you want data faster, just do a registerForTemporalEvent() for 5 minutes, and when you get the data, switch back to 60 minutes. You'll likely want to do the switch back in the background process itself, as the WF may not be active at the time and you don't want the every 5 min requests to continue.

    In some of my apps, changing the frequency of the requests is actually one of the settings, and I don't event register for temporal events until the info I need is in settings (a "key", etc).
  • Former Member
    Former Member over 7 years ago
    What isn't working for you with getLastTemporalEventTime()?

    Another option would be to set a new temporal event using a 5 minute duration. If it has been more than 5 minutes since your last temporal, that event would trigger immediately. Once you get the update you can re-register again with the 1 hour duration.
  • I am using the code as in the SDK documentation. See below.

    Using const throws me a build error ("no viable alternative at input const'")
    If I change const to var, I get an error while running the settings update ("Unhandled Exception
    UnexpectedTypeException: Expected Method, given Class definition").

    I can try Jim's method above, but I did like the concept of the sdk sample.



    using Toybox.Background;
    using Toybox.Time;
    const FIVE_MINUTES = Time.Duration(5 * 60);
    var lastTime = Background.getLastTemporalEventTime();
    if (lastTime != null) {
    // Events scheduled for a time in the past trigger immediately
    var nextTime = lastTime.add(FIVE_MINUTES);
    Background.registerForTemporalEvent(nextTime);
    } else {
    Background.registerForTemporalEvent(Time.now());
    }
  • Doing the new registerForTemporalEvent() for 5 minutes that Brian and I mentioned will have the same effect as using getLastTemporalEventTime in the API doc, in that if it's been more than 5 minutes after the last temporal event, it will run right away. If not, it will be 5 minutes after the last temporal event. Temporal events can run at most every 5 minutes, and there's no way around that.

    If you use getLastTemporalEventTime() in the background, I just checked, and it's always "now", so that can't be used to see if the last one was 60 minutes ago or 5 minutes ago when it comes to resetting.
  • The error is on this line...

    const FIVE_MINUTES = new Time.Duration(5 * 60);


    Note that the new keyword is missing. I'll fix the docs.

    As for the first error, you can only declare const variables at global/module/class scope. You can't declare a const inside a function body. To keep the samples small, we show things (using statements and const declarations) that should occur at global/module scope and other things (application logic) in the same code snippet. You have to separate them yourself.
  • Got it to work. Thanks all for your help!