Occasionally onBackgroundData is not invoked when background.exit()

Hi, I have a watch face that is using makeWebRequest in a background to get data from a web server. Occasionally, the wath face stops update the data in the UI, this usually happens after connecting it to the USB, or after exiting the morning report, and sometimes it just happens.

Whar Ican see in the log files is that the background process fires up as it should, it makes the webRequest and the callback function gets invoked with the data and responseCode. In the web request callback function function readResponse(responseCode, data) as void I can see that I got good data and I call Background.exit(myData);

When things are working as they should, the on BackgroundData(data)function gets invoked and I get the data to the foreground so I can update th UI. 

Here is the log entries when it works:

T:15:19:33 app:onStart
T:15:19:33 in getServiceDelegate
T:15:19:33 Background Service initialize()
T:15:19:33 in onTemporalEvent
T:15:19:33 in getData
T:15:19:36 in readResponse: [<my data here>] 200
T:15:19:36 in readResponse: bg_exit with data
T:15:19:36 in onBackgroundData: [<my data here>]

Her is the logging when it doesn't work:

T:14:41:32 app:onStart
T:14:41:32 in getServiceDelegate
T:14:41:32 BG Service initialize
T:14:41:32 in onTemporalEvent
T:14:41:32 in getData
T:14:41:35 in readResponse: [my data here] 200
T:14:41:35 in readResponse: bg_exit with data
T:14:41:35 app:onStop

When the problem starts, the only way to get it working again is to restart the watch, or toggling to another watch face and then toggle back. 

The problem not that the background process fires, it always does, the problem is that the onBackgroundData in the main app does not get called when I do Background.exit() from the background

I have completetly run out of ideas of things to test. Anyone seen this before? could it be something happening in the system that disconnects the background process from the app, or something? I'm clueless. 

I have no crashes and no crash file on the target. 

I can of cours edump all my code here but wanted to see if anyone had any idea first. 

as always, thanks for your help. 

 

  • I use on-demand temporal events, not reoccurring events. But I supposed if I registered for a single event and due to the 5 min policy, it was scheduled to run say 4 minutes later.... then even if your primary data field terminates (say you switch to a profile that doesn't have he DF), the BG process will still run. Upon exit() I assume it would just die and not cause any impact on anything else currently running?

  • The thing ERA won't tell you in the case of the background, is timing.  Has the main app run before or only after the background service runs.

    I've been using background apps pretty much since the day it was announced, so 8 years,  The only time I've seen onBackgroundData fail to be called is when a null was passed for data, the background crashed, or I tried to return more data than allowed (about 8k)

    And of course, onBackgroundData doesn't get called if the main app isn't running as that only gets called in the main app.

  • Hi Jim, the main app creates the background event every five minutes. This is a watch face and the time on the watch face updates as it should, so I assume the FG process is running. The issue typiccaly occurrs when the watch has been offline (not connected to the Garmin connect Mobile) for some time. I have never seen it happened when the watch is BT connected. 

    The watch face still fires off the BG process every five minutes, as can be seen in my log files, and the BG process does what it is supposed to, but there is no one on the other side (the FG process) that picks up the background data. Since the time updates on the watch face, I know that the view:onUpdate() function executes as it should, hence the FG process is still running. 

    Now I admint I don't know the ins and outs of a watch face life cycle so not sure if there is anything I didn't hink of. But I cannot come up with any obvious problem in my code. 

    Here is what the app does (on a high level):

    The app initiates and registers a temporal event. The background event calls makewebrequest to fetch some data from the Internet. The makewebrequest callback function receives the data, reformats it, and then sends it to the FG process via background.exit(). The data is received in the FG process by onBAckgroundData() in my app class. I then put the data in a global variable to make it available for the view class to disply the data on the watch face. After receiving the data in onBackgroundData(), I register a new temporal event that will run again after five minutes. The watch face view, displays time of day plus the data that is passed on from the BG process.   

    In most of the time, the data is received by the onBackgroundData() function in the app class. And the data can then be used by the view to display on the watch face. When the problem occurr, the time of dayt continues to update but the data from the internet is not updated. I can see that the background process exits (with data) but onBackgroundData() does not get invoked. And so it continues, every five minutes, until I either restart the watch, or toggle to another watch face and back. Very confusing and annoying (have been trying to solve this for a long time now)  :-)