昨天,我问,因为我的计时器在后台有问题,我找到了一个解决方案,使其工作,但现在,当我的应用程序进入后台时遇到问题。在后台ApplicationDidEnterBackground发生意外的崩溃:NSTimer
运行:
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
})
这是我的计时器:
let interval = 0.01
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
这是我updateTimer功能:
func updateTimer() {
var j = 0
for _ in rows {
if (rows[j]["Playing"] as! Bool == true) {
rows[j]["time"] = (rows[j]["time"] as! Double + interval) as AnyObject
rows[j]["lastTime"] = (rows[j]["lastTime"] as! Double + interval) as AnyObject
}
if (rows[j]["lastTime"] as! Double > 60.0) {
min[j] += 1
rows[j]["lastTime"] = 0.00 as AnyObject
}
j += 1
}
}
在我applicationDidEnterBackground方法我调用这个函数:
func backgroundTimer() {
print("background") // This Print works fine
timer.invalidate() // Never works
interval = 1.00 // Crash when interval change from 0.01 to 1.00
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)
}
这是我的输出,当我的应用程序在后台输入:
的是双间隔。
In Info.plist我补充:应用程序不会在后台运行:NO。
让我知道我做错了什么?
编辑:行
初始化在viewDidLoad方法
let i: [String : AnyObject] = ["time": time as AnyObject, "Playing": false as AnyObject, "lastTime": 0.00 as AnyObject, "lapNumber": 0 as AnyObject, "min": 0 as AnyObject]
rows.append(i as [String : AnyObject])
rows [j] [“Playing”]的值显然为零。 –
@AmeyaVichare感谢您的回答。它不能为零,它不是前景零,我不明白为什么它可以在后台 – pierreafranck
你如何初始化行?请张贴代码... –