我有一个应用程序在前台以及后台使用位置更新。使用CoreLocation框架,我已经实现了该应用程序,以便每隔5分钟将位置更新发送到服务器,并使用this代码作为参考。iOS为什么系统使用位于后台的应用程序杀死应用程序
这在前台运行良好,但是当应用程序进入后台时,它会在30分钟到一个小时后被操作系统杀死。我希望应用能够获得至少8小时的更新,即使在后台也是如此。
此外,该应用程序每小时使用约10%的电池。这与应用程序在后台死亡有关吗?如果是这样,那我该如何解决电池问题?否则,谁能告诉我这是什么问题?
下面是崩溃日志的设备:
Exception Type: 00000020
Exception Codes: 0x000000008badf00d
Exception Note: SIMULATED (this is NOT a crash)
Highlighted by Thread: 2
Application Specific Information:
<BKNewProcess: 0x17e74840; com.app.app; pid: 560; hostpid: -1> has active assertions beyond permitted time:
{(
<BKProcessAssertion: 0x17d78740> id: 560-C9E81E97-90D9-4F95-871E-3DC53372F302 name: Called by UIKit, from <redacted> process: <BKNewProcess: 0x17e74840; com.app.example; pid: 560; hostpid: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:560 preventSuspend preventIdleSleep preventSuspendOnSleep ,
<BKProcessAssertion: 0x17e6a870> id: 560-BD7B29FC-DABC-42FF-AF17-B277BDB1C59D name: Called by UIKit, from <redacted> process: <BKNewProcess: 0x17e74840; com.app.example; pid: 560; hostpid: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:560 preventSuspend preventIdleSleep preventSuspendOnSleep
)}
对于我用下面的功能,后台任务:
func backgroundTask(){
var application=UIApplication.sharedApplication()
var background_task: UIBackgroundTaskIdentifier?
background_task = application.beginBackgroundTaskWithExpirationHandler({() -> Void in
application.endBackgroundTask(background_task!)
background_task = UIBackgroundTaskInvalid
})
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in
//run the app without startUpdatingLocation. backgroundTimeRemaining decremented from 600.00
self.locationManager.startUpdatingLocation()
while (true) {
//backgroundTimeRemaining time does not go down.
print("Background time Remaining: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
NSThread.sleepForTimeInterval(1)
break
//wait for 1 sec
}
application.endBackgroundTask(background_task!)
background_task = UIBackgroundTaskInvalid
})
}
我已经更新了这个问题。拥有崩溃日志。 – Poonam
8badf00d以“应用程序花费太长时间才能启动,终止或响应系统事件而闻名”。 据我所知,从登录线程2东西需要太长的时间来执行主线程,它会导致终止 –
以及如果您的应用程序不断在后台运行,为什么不考虑使用后台模式。我认为这比背景任务更好并保持较长的背景。 –