iPhone background the RIGHT way

You must Login before you can answer or comment on any questions.

Hi there -- what's the proper way to use backgrounding in iOS and avoid these types of forced crashes:

Mar  7 16:32:41 unknown SpringBoard[70] <Warning>: app[4044] has active assertions beyond permitted time: 
    {(
        <SBProcessAssertion: 0x105b75b0> identifier: UIKitBackgroundCompletionTask process: app[4044] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:4044 preventSuspend  preventIdleSleep ,
        <SBProcessAssertion: 0x103ec500> identifier: UIKitBackgroundCompletionTask process: app[4044] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:4044 preventSuspend  pre...
I have this in my app.js:
var service = Titanium.App.iOS.registerBackgroundService({
    url: 'backgroundservice.js'
});
and in backgroundservice.js I have this:
Ti.API.info('Background service started');
 
// now unregister it in 590 seconds (under 600 to be safe!)
var stopTimeout = setTimeout(function() {
    stopTimeout = null;
    Ti.API.info('Background service stopping...');
 
    Ti.App.currentService.stop();
 
    Ti.API.info('Background service stopped!');
}, 590 * 1000);
 
Ti.App.currentService.addEventListener('stop', function() {
    if(stopTimeout) {
        clearTimeout(stopTimeout);
    }
    Ti.API.info('Background service stopped automatically!');
});
Should I put an "unregister" somewhere do avoid crashes? (I DO want it to always go in the background, and allow background stuff to happen -- like finishing file uploads, etc.)

Thanks for your help in advance!

1 Answer

Accepted Answer

Try this:

Ti.API.info('Background service started');
 
// now unregister it in 590 seconds (under 600 to be safe!)
var stopTimeout = setTimeout(function() {
    // stopTimeout = null;//--- TODO: Change here
    Ti.API.info('Background service stopping...');
 
    Ti.App.currentService.stop();
 
    Ti.API.info('Background service stopped!');
}, 2000);
 
Ti.App.currentService.addEventListener('stop', function() {
    if(stopTimeout) {
        clearTimeout(stopTimeout);
        stopTimeout = null;
    }
    Ti.API.info('Background service stopped automatically!');
});

— answered 1 year ago by Minh Nguyen
answer permalink
1 Comment
  • I think this is right -- basically stop the process sooner. The only issue is if they're still uploading files... But I think 8 minutes is long enough (heh, 1 minute is probably long enough!) Thanks.

    — commented 1 year ago by Elijah Windsor

Your Answer

Think you can help? Login to answer this question!