Persistent Background Tasks?

I think I remember reading that is a data field registered a background task to fire every X minutes (or whatever)... that the task persists across device power cycles and even if the data field is removed. I'm getting some unexpected behavior from my Edge device that I think is from old background tasks I no longer use or want and no longer have the DF installed. If this is true, is there a function I can run to deregister/purge and of these zombie tasks?

I'd prefer not to do a hard reset and then have to re-pair all my sensors, etc.

Thanks!

  • Yes, if you don't do anything special, when you have a background services in a data field, it will keep going after the activity is exited.  Same as it would with a widget..

    What I do, is in my AppBase, I have a variable called inBackground that I set to false,  And get it to true in in getServiceDelegate (that runs only in the background).  Then in onStop, if it's false, I delete the temporal event.  That stops the background service when the DF isn't running.  Here's the basics:

    class PiDfApp extends Application.AppBase {
    	var inBackground=false;
    
        function initialize() {
            AppBase.initialize();
        }
    
        function onStart(state as Dictionary?) as Void {
        }
    
        function onStop(state as Dictionary?) as Void {
        	if(!inBackground) {
        		Background.deleteTemporalEvent();
        	}
        }
    
        function getInitialView() as Array<Views or InputDelegates>? {
        	if(Toybox.System has :ServiceDelegate) {
        		Background.registerForTemporalEvent(new Time.Duration(5 * 60));
        	}    
            return [ new PiDfView() ] as Array<Views or InputDelegates>;
        }
    
        function getServiceDelegate(){
        	inBackground=true;
            return [new PiDfServiceDelegate()];
        }    
    
        function onBackgroundData(data) {
            ...
        }
    }

  • Thx Jim. I get that it keeps running after the activity is stopped/saved. But it even persists across power cycles? If so, I simply want to purge any zombie tasks. One time thing. With the new Weather feature, I no longer use BG services. I could add that onStop logic to a DF I use to kill off any zombies and then comment out that code I guess.

  • The code I posted is a stripped down AppBase I use in a DF talks to a Raspberry Pi using BLE.

    The Pi beeps when something connects to it, and without the deleteTemporal event, it would beep after I exited the activity, and across device boots.

    It relies on the fact that the background and the main DF each have their own copy of inBackground and only the inBackground in the background service is set to true. 

  • Wow - very cool application! So it sounds like it would be easier (in this case) to just hard reset my device than write a reaper to go find/kill any zombie tasks.

  • With what I posed, there are no zombies, no need to reset anything.  When the DF exits, everything is left clean, and when you run the DF again, it starts clean.

  • Yep! If/when I need to use a BG tasks again, I'll use your approach. That is GREAT. In my case, it seems I have old zombies living in the background and need to kill those suckers once and for all.