gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:05:57 24 4
gpt4 key购买 nike

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

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

}

最佳答案

当您的应用进入后台状态时切换到重要位置更新,您的应用将持续接收位置更新。我认为你可以在 CLLocationManger 的对象上调用 startMonitoringSignificantLocationChanges。我认为你也不需要建立后台任务。

检查 Apple Documentation , 它指出,

If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services

所以,我认为它会解决您的问题,也会解决电池问题。

第二件事(为了电池消耗),当你想在后台长时间更新位置时,你不应该将 DesiredAccuracy 设置为 kCLLocationAccuracyBest。您可以将 kCLLocationAccuracyThreeKilometers 设置为 DesiredAccuracy 并且您可以在后台输入时将 setDistanceFilter 设置为非常大的数字,例如 99999

可以引用this so postthis so post .

希望这会有所帮助:)

关于iOS 为什么系统在后台使用位置杀死应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com