[英]Prevent iOS from killing App after a few minutes
I'd like to prevent iOS from killing my app after a few minutes.我想阻止 iOS 在几分钟后杀死我的应用程序。 I've read this thread here on SO: Prevent iOS from killing my app after 3 minutes .
我已经在 SO 上阅读了这个线程: 防止 iOS 在 3 分钟后杀死我的应用程序。 It says that if I have no backgroundtasks longer than 3 minutes my app wont be killed.
它说如果我没有超过 3 分钟的后台任务,我的应用程序不会被杀死。 Can someone verify that this is true?
有人可以验证这是真的吗? Because my background-task is not running longer than 3 minutes and even though my app gets killed after this time.
因为我的后台任务运行时间不超过 3 分钟,即使我的应用程序在此时间之后被杀死。 My background-task is a timer that updates a widget.
我的后台任务是一个更新小部件的计时器。 Heres some code:
继承人一些代码:
self.backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.endBackgroundTask()
//endBackGroundTask looks like this
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = UIBackgroundTaskInvalid
//
}
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)
. .
// at the beginning of the class
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
. .
// in viewWillDisappear
self.timer.invalidate()
if self.backgroundTask != UIBackgroundTaskInvalid {
self.endBackgroundTask()
}
You need to structure your app so that it doesn't require continual execution in the background.您需要构建您的应用程序,使其不需要在后台持续执行。 As I understand it, your app shows a count down timer and can show the same count down timer in a Today Widget.
据我了解,您的应用程序显示倒计时计时器,并且可以在“今日”小部件中显示相同的倒计时计时器。 The approach I would use is follows:
我将使用的方法如下:
Timer
to periodically update your UITimer
定期更新您的 UITimer
in the widget to periodically update its UITimer
来定期更新其 UI If you do this then you never need to call beginBackgroundTask
.如果你这样做,那么你永远不需要调用
beginBackgroundTask
。 If you do call beginBackgroundTask
and don't call endBackgroundTask
within 3 minutes of entering the background, then your app will be terminated, even if you aren't using any CPU.如果您确实调用了
beginBackgroundTask
并且在进入后台的 3 分钟内没有调用endBackgroundTask
,那么您的应用程序将被终止,即使您没有使用任何 CPU。
Short answer: You can't run a background task for longer than 3 minutes unless you are a turn-by-turn navigation app or an audio player.简短回答:除非您是逐向导航应用程序或音频播放器,否则您不能运行超过 3 分钟的后台任务。 Apple doesn't allow it by design.
Apple 设计上不允许这样做。
Your background task is a timer that is running longer than 3 minutes.您的后台任务是一个运行时间超过 3 分钟的计时器。 So your app is correctly being killed.
所以你的应用程序被正确地杀死了。 Consider it confirmed as that is Apple's design.
考虑确认这是苹果的设计。
It's not what your timer is executing that is killing the app, it's the timer itself.杀死应用程序的不是您的计时器正在执行的操作,而是计时器本身。
You can read up on Apple's Documentation for more information.您可以阅读Apple 的文档以获取更多信息。
Always try to avoid doing any background work unless doing so improves the overall user experience.
始终尽量避免做任何后台工作,除非这样做可以改善整体用户体验。 An app might move to the background because the user launched a different app or because the user locked the device and is not using it right now.
应用程序可能会移至后台,因为用户启动了不同的应用程序,或者因为用户锁定了设备并且现在没有使用它。 In both situations, the user is signaling that your app does not need to be doing any meaningful work right now.
在这两种情况下,用户都在表示您的应用程序现在不需要做任何有意义的工作。 Continuing to run in such conditions will only drain the device's battery and might lead the user to force quit your app altogether.
在这种情况下继续运行只会耗尽设备的电池,并可能导致用户完全强制退出您的应用程序。 So be mindful about the work you do in the background and avoid it when you can.
因此,请注意您在后台所做的工作,并尽可能避免它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.