在后台运行计时器 [英] Running a Timer in the background

查看:101
本文介绍了在后台运行计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Swift中编写一个应用程序,其中的计时器倒计时,很像倒计时时钟.为此,我在主逻辑类中使用以下代码:

I'm writing an app in Swift where a timer counts down, much like a countdown clock. To do this I am using this code in my main logics class:

func start() {
    self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
        self.run()
    }
}

现在,每次我关闭应用程序时,计时器都会停止并且出现此错误:

Now every time I close the app, the timer stops and I get this error:

BackgroundTask:不存在标识符为1(0x1)的后台任务,或者它可能已经结束.

BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended.

有什么方法可以继续在后台运行计时器吗?还是您有其他可能更优雅的方式来解决我的问题?我已经在整个stackoverflow上搜索了几个小时,但是似乎找不到答案.

Is there any way to continue the timer running in the background? Or do you have any other, possibly more elegant, way to solve my problem? I have searched all over stackoverflow for this for hours now, but I can't seem to find an answer.

推荐答案

在您的应用程序后台运行后,Apple不允许您长时间运行进程.从文档中:

Apple will not allow you to run processes for long periods of time after your app has been backgrounded. From the docs:

执行长期运行的任务

对于需要更多执行时间才能实现的任务,您必须请求特定权限才能在后台运行它们而不暂停它们.在iOS中,只允许特定的应用类型在后台运行:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • 在后台播放可听内容的应用程序,例如音乐播放器应用程序
  • 在后台录制音频内容的应用
  • 始终使用户了解其位置的应用程序,例如导航应用程序
  • 支持互联网协议语音(VoIP)的应用
  • 需要定期下载和处理新内容的应用
  • 从外部附件接收定期更新的应用
  • 实现这些服务的应用程序必须声明它们支持的服务,并使用系统框架来实现这些服务的相关方面.
  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories
  • Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services.

声明服务可使系统知道您使用的服务,但是在某些情况下,实际上是系统框架阻止了您的应用程序被挂起.

Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

话虽如此,如果您真的只是在尝试更新倒数时钟",我将在应用程序后台运行时简单地存储当前系统时间(也许在UserDefaults中).然后,当应用程序回到前台时,将存储的时间与当前时间进行比较,并执行必要的计算以确定您的时钟应显示什么时间.

With that said, if you're really just trying to update a "countdown clock," I would simply store the current system time (perhaps in UserDefaults) at the time the app is backgrounded. Then, when the app is brought back to the foreground, compare the stored time to the current time and perform the necessary calculations to determine what time your clock should display.

这篇关于在后台运行计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆