当我的应用程序在 iOS8 上处于后台模式时,我想运行一个周期性任务。我快速编写了以下代码但失败了。有人能指出哪里出了问题吗?
这是我来自 AppDelegate.swift 的代码
let backgroundQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND,0)
func applicationDidEnterBackground(application: UIApplication) {
println("did enter background")
dispatch_async(self.backgroundQueue, myBackgroundTask)
}
func myBackgroundTask() {
NSThread.sleepForTimeInterval(0.5)
println("this is back ground task")
dispatch_async(self.backgroundQueue, myBackgroundTask)
}
最佳答案
有人告诉我调用“beginBackgroundTaskWithName”就是答案。
func applicationDidEnterBackground(application: UIApplication) {
println("did enter background")
application.beginBackgroundTaskWithName("myBgTask", expirationHandler: nil)
dispatch_async(self.backgroundQueue, myBackgroundTask)
}
func myBackgroundTask() {
NSThread.sleepForTimeInterval(0.5)
println("this is back ground task")
dispatch_async(self.backgroundQueue, myBackgroundTask)
}
关于swift - 在 iOS8 上以后台模式运行周期性任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338027/