onBackgroundData not called

I searched the forum, and read all the old threads related to onBackgroundData, but I can't find out why it's not working.

(:background)
class MyServiceDelegate extends System.ServiceDelegate {

	function initialize() {
		Background.exit("this works");
	}
	
	function onTemporalEvent() as Void {
		log("onTemporalEvent");
		// Background.exit("test1");
		doTheWork();
		// Background.exit("test2");
	}
}

My problem is that onBackgroundData is not being called after I call Background.exit(non null String or Number); The only place where it works is when I call it from the constructor of the ServiceDelegate, which of course makes no sense, because then the job didn't even start yet, but at least it proves that the onBackgroundData in the foreground app is OK.

When I call Background.exit("str") from anywhere else (my real place is the onReceive callback of makeWebRequest, but even if I call it from the beginning of onTemporalEvent) onBackgroundData is not called. What can be the culprit?

SDK 4.1.7, simulator, fr255, datafield

  • His code works on condition the comma will be removed from annotation:

    (:background , :background_app)

    class StatsServiceDelegate extends System.ServiceDelegate

    ...

    I don't know what compiler does in such case but surely code doesn't run well.

    I tried that and it doesn't work for me. The output is identical to the build with the comma.

    If Monkey C was so broken that adding a comma between two annotations would (silently) break the build, I would probably give up.

  • After removing comma I can see

    1:29:27 TestApp.onBackgroundData !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: -101

    but I use eclipse and win 

  • Interesting. I used VS Code and Windows, with SDK 4.1.7, and that change made no difference for me.

    As I mentioned above, I think there's some weird timing/build issue that causes the code to fail, since I could sometimes make it work by changing some "random" things.

    I also noticed that maybe it has something to do with how long the background process has been running in the sim. Like I said, one issue is that when you run the app for the first, there are two makeWebRequests (one for app startup and one for onTemporalRequest), but the response callback is only invoked once, because the background process exits after the first invocation. I don't know if that muddies the waters as well.

  • Also, I did a binary compare of 2 prgs: one built with the comma, and one built without the comma. They're identical.

    So I don't see how removing the comma can make a difference. More likely the real difference was running the app for a 2nd time while the sim was already open and background process was already running.

  • the comma is irrelevant, I have comma between all  my annotations, I didn't eve know the comma is optional. However the comma and the :background_app annotation is irrelevant, I forgot to remove it for the POC, but since the excludeAnnotation background_app isn't added in the jungle it has no effect.

    I also wrote you already that it works because you get a negative error code. I don't know why it does work then but it does, however it's an edge case, and I'd like it to work when the response code is 200

  • Not quite sure what your goal here is, but I added some stuff the the test df app I posted a few days back.

    In this one, I set a flag in Storage if onAppUpdate was called.  In the sim, that happens every time you re-compile but on a real device, only when you publish an update and that update gets sent to the device)

    I set the flag by way of Background.exit() in onAppUpdate as on some devices you can't change Storage directly in the background.

    The next time there is a temporal event, the data is returned or the error is returned.  Both by Background.exit().

    When the main app sees the data (in onBackgroundData) it clears the flag if there was no error, but if there was an error (say -104 if there's no BLE connection) it will keep retrying the request each time the temporal event runs.  

    Once there has been good data received in the main app and the flag as been cleared, the background goes back to returning the current minute and there are no more makeWebRequest calls.

    3276.dfmin.zip

    .

  • What you do is what probably I would up doing (or something similar), but seriously, this is gettin ridiculous: instead of doing a webRequest in the onAppUpdate you do a Background.exit because you can't do a webRequest, and you also can't use storage... to pass something to the foreground, there you add it to the storage and trigger a temporal event, to pick up the data from the storage, which you can't delete in the background so you call Background.exit with some other structure to indicate that foreground can delete the data from the storage. And of course this depends on the user also starting the foreground app...

    My goal was to do some statistics, but given the old devices I support probably I'll give up on that for this app, and only add to some new app that will not have to deal with the old CIQ apis