iOS为什么系统在后台使用位置杀死应用程序

Poo*_*nam 5 core-location ios swift location-services

我有一个应用程序,它在前台和后台使用位置更新.使用CoreLocation框架,我已经实现了应用程序,以便每隔5分钟将位置更新发送到服务器,使用代码作为参考.

这在前台工作正常,但当应用程序进入后台时,它会在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 
)}
Run Code Online (Sandbox Code Playgroud)

对于后台任务,我使用以下函数:

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
    })

}
Run Code Online (Sandbox Code Playgroud)

Ali*_*ran 1

您是否有任何崩溃日志。如果应用程序因某些隐藏错误而未终止,您应该怀疑内存压力。我认为本文将引导您找到突然终止的原因

https://www.raywenderlich.com/23704/demystifying-ios-application-crash-logs