我正在开发一个应用程序,需要每30分钟更新位置的后台任务。我尝试使用NStimer并每分钟更新一次,它可以工作(花费了2个小时)。然而,当我将其设置为30分钟时,iOS在10分钟后将我的应用程序关闭。
日志显示:
Sun Oct 16 11:29:47 unknown SpringBoard[32] <Warning>: MyApp[315] has active assertions beyond permitted time:
{(
<SBProcessAssertion: 0xbb34040> identifier: UIKitBackgroundCompletionTask process: MyApp[315] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:315 preventSuspend preventIdleSleep
)}
Sun Oct 16 11:29:47 unknown SpringBoard[32] <Warning>: Forcing crash report of WhereAreYouReg[315]...
Sun Oct 16 11:29:48 unknown SpringBoard[32] <Warning>: Finished crash reporting.
我的后台任务是通过以下代码启动的:
backgroundTaskIdentifier = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
// If you're worried about exceeding 10 minutes, handle it here
}];
theTimer=[NSTimer scheduledTimerWithTimeInterval:30*60.0
target:self
selector:@selector(updateLocation)
userInfo:nil
repeats:YES];
updateLocation开始更新位置,当接收到位置后,它会停止更新位置以节省电池。
如何执行此后台任务?有什么帮助可以防止我的应用程序在600秒后被杀死吗?
非常感谢!