[TIMOB-4875] iOS: Background services do not run when the app is locked
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2011-09-14T13:04:52.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Sprint 2011-37 |
Components | iOS |
Labels | n/a |
Reporter | Evan Borgstrom |
Assignee | Reggie Seagraves |
Created | 2011-08-01T05:26:40.000+0000 |
Updated | 2017-03-23T21:38:10.000+0000 |
Description
When running your app in the foreground, if you press the home button then background services work as expected, however if you "Lock" the device (CMD+L in the simulator, top button on the device) then the background code never runs.
Here's how to reproduce it in two simple files that can be dropped into a new project.
app.js:
var w = Ti.UI.createWindow();
var bgsvc = Ti.App.iOS.registerBackgroundService({ url: '/bg.js' });
Ti.App.addEventListener('pause', function(e) {
Ti.API.debug("pause fired");
});
Ti.App.addEventListener('resumed', function(e) {
Ti.API.debug("resumed fired");
});
w.open();
bg.js:
Ti.API.debug("Hi from bg.js, about to loop forever");
function bgfunc() {
setTimeout(bgfunc, 5000);
Ti.API.debug("Hi from bgfunc");
}
bgfunc();
Running the above code I see the following when pressing the home button:
{noformat}
[INFO] bgtest/1.0 (1.7.1.1293a6d)
[DEBUG] Analytics is enabled = YES
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/
app.js, resource: Users/evan/Projects/Titanium/bgtest/Resources/app_js
[DEBUG] application booted in 28.014004 ms
[DEBUG] fire app event: pause
[DEBUG] pause fired
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/bg.js,
resource: Users/evan/Projects/Titanium/bgtest/Resources/bg_js
[DEBUG] Hi from bg.js, about to loop forever
[DEBUG] Hi from bgfunc
[DEBUG] Hi from bgfunc
... continues every 5 seconds ...
[DEBUG] fire app event: resumed
[DEBUG] resumed fired
{noformat}
But if I run the app and push the lock button I get:
{noformat}
[INFO] bgtest/1.0 (1.7.1.1293a6d)
[DEBUG] Analytics is enabled = YES
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/
app.js, resource: Users/evan/Projects/Titanium/bgtest/Resources/app_js
[DEBUG] application booted in 27.069032 ms
[DEBUG] fire app event: pause
[DEBUG] pause fired
... wait 10 seconds, unlock ...
[DEBUG] fire app event: resumed
[DEBUG] resumed fired
{noformat}
I have found a similar question on the developers site: http://developer.appcelerator.com/question/116667/ios-background-geolocation-in-16
In the second comment there is a link to a github URL that contains a fork of titanium: https://github.com/mpociot/titanium_mobile/commit/4853f091639e899545f9d1ef1416b1ca5baa0746
The code in the fork seems to address the issue and enables background services to run on lock. Can this be merged into Titanium?
Evan It's so nice to see a well-thought-out ticket like this one. Thank you for raising it. I note that your logs show that you tested your code on 1.7.1.1293a6d but in the environment field you have entered Titanium SDK 1.7.2. Would you mind entering all the versions you have tried into the environment field, in the format that includes the build hash (like 1.7.1.1293a6d does). Then I can move this ticket to the correct project. Cheers
Hi Paul, My pleasure. Happy to contribute back. I've been playing with Titanium for about 18 months now, but have just undertaken my first real project with it. I've updated the environment field to include the exact build versions of the two SDK's I've tested against.
Bumped priority.
There seems to be a fix to this issue: https://github.com/mpociot/titanium_mobile/commit/4853f091639e899545f9d1ef1416b1ca5baa0746
This is actually working properly as designed. When the device is locked, the application is not changed to the background state and foreground processes still run. We therefore properly do no fire off the background tasks.
Closing ticket as invalid with reference to the previous comments.