Temporal Background Termination

This was unexpected...

I set up a Temporal BG service to run every 5 minutes to grab the Device Temperature.

Even before I hit START to begin an Activity on my EDGE... after less than 5 minutes, I'll have the Device's temperature. Great.

Then when I hit START to begin the activity, I still have access to DEVICE Temp, which updates every 5 minutes. Great.

However, if at some point I hit STOP, then RESUME the activity, I lose DEVICE Temp, and never get it back. This is not uncommon for users to use the stop/resume buttons.

As I understand it... tapping the STOP button triggers the onTimerStop() method. Then the onTimerResume() method upon resuming. Great.

But apparently tapping the STOP button ALSO triggers the onStop() method in Application.AppBase. This is where I had put this code in order to try to reap the background process so that it didn't continue to run on my EDGE device forever, as a zombie process, even if my data field wasn't running.

function onStop(state) { if (isBackground == true) { deleteTemporalEvent(); } }

Questions:

 - Is reaping the process to prevent it from becoming an enduring zombie an issue? Or do these die when the device is powered off and back on?

 - If so, how would I terminate the BG process at the activity end/save (not at a STOP, which can be resumed)?

Thanks!

  • 1) When a device is sleeping or powered off the BG process is not being invoked. I checked it once using simple System.println() statements with current time value. Not sure, however, about the case when datafield is not added to the screen or when another activity profile is selected. I believe the BG shouldn't be invoked in these cases as well.

    2) onTimerReset()

    Also, I didn't get your code 

    function onStop(state) { if (isBackground == true) { deleteTemporalEvent(); } }

    why are you trying to kill a BG process from the BG process itself? Looks like you need to remove a temporal event by calling from the main app instance (condition !isBackground)

  • onStop() is called when the background service terminates as well as when the main app terminates.  "IsBackground" is a trick so that the temporal event is only deleted if the main app terminates, not when the background service terminates..

  • Yes, I understand this concept, I meant that the flag in Dave's code should be inverted if he wants to delete a temporal event from the main app instance.

  • if inBackground defaults to false, and then set to true in getServiceDelegate, the code is fine.  Remember, the main app and the background have their own copies of isBackground.  

  • If isBackground is set to true in getServiceDelegate() then deleteTemporalEvent() will be executed from BG's onAppStop() method, not from the main instance's onAppStop().

  • onStop() is in AppBase and called by the system.  There is no standard call "onAppStop"  Do you mean onTimerStop?  That also occurrences when you pause an activity 

  • I meant onStop() from the AppBase class.

    function onStop(state) { if (!isBackground) { deleteTemporalEvent(); } }
    That should be the correct version of Dave's code which will kill the backgrounding upon the main app exit (but not on timer stop of course).
  • The intent of my code was to only kill the BG process upon the completion of the BG process. Which might be redundant? But I wanted to be sure the BG process died and didn’t stick around as a zombie after my activity is done. I need it to run every 5 mins during the activity.

  • Here is a flow you have now:

    1) The main app is started upon downloading it from the store and adding datafield to the screen.

    2) Backgrounding is being scheduled right after the installation by calling registerOnTemporalEvent() inside the getInitialView() method.

    3) Backgrounding is stopped forever just after the first invocation, because you call deleteTemporalEvent() inside the BG instance onStop() method.

    What you really need is something like

    1) Call registerOnTemporalEvent() inside the onTimerStart() method.

    2) Call deleteTemporalEvent() inside the onTimerReset() method.

    Therefore, backgrounding would be executed only during the activity (including timer pauses).

  • But if you finish an activity and delete the temporal event and start another activity with the same DF right away, the 5 minute rule still applies, so it could be 5 minutes after you do the register before the background service runs.  Just something to keep in mind.