Five Min BG scheduling

I think this should work... in my onTimerStart() method once an Activity is started. I could put this in onStart() instead so it does this even before the Activity starts?

What I think this should do is fire off my BG process immediately if the last time didn't exist or is more than 5 mins ago.

Since that is a MOMENT, that is a one time trigger. Then go ahead and also setup an EVERY FIVE MINS Duration event, regardless of if I have started an immediate event or not.

Any timing issues with this logic?

                //////////////////////////

                var lastTime = Background.getLastTemporalEventTime();

                var FIVEMINS = new Time.Duration(5 * 60);
                if (lastTime == null || Time.now().greaterThan(lastTime.add(FIVEMINS))) {
                    System.println("Background Event triggered right now");
                    Background.registerForTemporalEvent(Time.now());
                }
                System.println("Background Event scheduled every 5 minutes");
                Background.registerForTemporalEvent(FIVEMINS);
  • I think you need to delete temporal event after first one time run (now) and then re-register it for every 5 minutes.

    Something like this:

    var firstTime=true;
    function onBackgroundData(data) {
    ... do something with data returned
        if(firstTime){
            Background.deleteTemporalEvent();
            registerBgEvent();
            firstTime=false;
        }
    }

  • I didn't understand what't the problem. Is it that after it runs immediately it doesn't run again 5 minutes later? Or that it doesn't run immediately even when the last time it ran was more than 5 minutes ago? If the latter then IMHO what happens is that you register it for now, then immediately you re-register it for 5 minutes from now, that's why it doesn't run immediately.

    BTW not sure if it's critical but IMHO you can "improve" it by registering not for 5 mins from now, but to (5mins - (now - lastTime)) in case it ran less than 5 minutes ago.

  • Don't do it in onStart() as that runs each time the background runs.  What you can do is put it in getInitialView as that only runs for the main app.

    What I typically do is always set a duration, so once started, it runs every X minutes for something like a widget or watch face, even if the main app is closed.  I avoid doing anything in onBackgroundData with the temporal event, as if the main app isn't running at that point, the background may not run again..  That's bad if you have a widget that publishes complications or something that only gets the weather every X minutes, like a watch face.

    Here's a thread from about 7 years back with a watch face with a backgound https://forums.garmin.com/developer/connect-iq/f/discussion/5287/very-simple-sample-of-a-watch-face-with-a-background-process

    And a mewer one with a whole bunch of stuff in addition to a simple background: https://forums.garmin.com/developer/connect-iq/f/discussion/349473/simple-example-wf-that-shows-a-bunch-of-things

    In both I just use a duration, and the backgound returns the time it ran, so it's easy to see if it's running when you expect.