Swift 如何防止短背景定时器漂移?
我有一个90秒计时器,它在后台运行,通过注册Swift 如何防止短背景定时器漂移?,swift,timer,background,Swift,Timer,Background,我有一个90秒计时器,它在后台运行,通过注册beginBackgroundTaskWithExpirationHandler完成。这似乎很简单,但我的问题是计时器本身漂移很大。对于90年代的计时器,我得到大约30-35秒的漂移。也就是说,如果我启动计时器,设置应用程序背景,然后在90秒后打开应用程序,计时器显示剩余30秒 如果我让应用程序在整个90年代都打开,我会得到零漂移。如果我将计时器上的时间间隔降低到1s(而不是我首选的0.05秒),则背景漂移消失 如何消除背景漂移而不降低计时器的精度 c
beginBackgroundTaskWithExpirationHandler
完成。这似乎很简单,但我的问题是计时器本身漂移很大。对于90年代的计时器,我得到大约30-35秒的漂移。也就是说,如果我启动计时器,设置应用程序背景,然后在90秒后打开应用程序,计时器显示剩余30秒
如果我让应用程序在整个90年代都打开,我会得到零漂移。如果我将计时器上的时间间隔降低到1s(而不是我首选的0.05秒),则背景漂移消失
如何消除背景漂移而不降低计时器的精度
class TimerViewController: UIViewController {
@IBOutlet weak var startTimerButton: UIButton!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var resetTimerButton: UIButton!
var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.05
let timerEnd:NSTimeInterval = 90
var timeCount:NSTimeInterval = 0
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
override func viewDidLoad() {
super.viewDidLoad()
resetTimeCount()
timerLabel.text = timeString(timeCount)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TimerViewController.reinstateBackgroundTask), name: UIApplicationDidBecomeActiveNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
@IBAction func startTimerButtonTapped(sender: UIButton) {
if !timer.valid { //prevent more than one timer on the thread
timerLabel.text = timeString(timeCount)
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self,selector: #selector(TimerViewController.timerDidEnd(_:)),userInfo: nil, repeats: true)
schedulePushNotification()
registerBackgroundTask()
}
}
@IBAction func resetTimerButtonTapped(sender: UIButton) {
timer.invalidate()
resetTimeCount()
timerLabel.text = timeString(timeCount)
cancelAllNotifications()
}
// MARK: Timer
func resetTimeCount(){
timeCount = timerEnd
}
func timerDidEnd(timer: NSTimer){
//timer that counts down
timeCount = timeCount - timeInterval
if timeCount <= 0 { //test for target time reached.
timerLabel.text = "Time is up!!"
timer.invalidate()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
if backgroundTask != UIBackgroundTaskInvalid {
endBackgroundTask()
cancelAllNotifications()
}
} else { //update the time on the clock if not reached
timerLabel.text = timeString(timeCount)
}
}
func timeString(time: NSTimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%02i",minutes,Int(seconds),Int(secondsFraction * 100.0))
}
// MARK: BackgroundTask
func registerBackgroundTask() {
backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
[unowned self] in
self.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func reinstateBackgroundTask() {
if timeCount != 0.0 && (backgroundTask == UIBackgroundTaskInvalid) {
registerBackgroundTask()
}
}
func endBackgroundTask() {
UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
// MARK: Notifications
func schedulePushNotification() {
let notification = UILocalNotification()
notification.alertAction = "Go back to App"
notification.alertBody = "The 90s timer is finished!"
notification.fireDate = NSDate(timeIntervalSinceNow: timerEnd+1)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func cancelAllNotifications() {
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
}
class TimerViewController:UIViewController{
@IBVAR弱启动按钮:UIButton!
@IBVAR时间标签:UILabel!
@IBOutlet弱var重置时间按钮:UIButton!
var timer=NSTimer()
let timeInterval:NSTimeInterval=0.05
让timerEnd:NSTimeInterval=90
变量时间计数:NSTimeInterval=0
变量backgroundTask:UIBackgroundTaskIdentifier=UIBackgroundTaskInvalid
var backgroundTaskIdentifier:UIBackgroundTaskIdentifier?
重写func viewDidLoad(){
super.viewDidLoad()
resetTimeCount()
timerLabel.text=timeString(timeCount)
NSNotificationCenter.defaultCenter().addObserver(self,selector:#selector(TimerViewController.RestoreBackgroundTask),名称:UIApplicationIDBecMeactiveNotification,对象:nil)
}
脱硝{
NSNotificationCenter.defaultCenter().removeObserver(自)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//马克:行动
@iAction func startTimerButtonTapped(发送方:UIButton){
if!timer.valid{//防止线程上有多个计时器
timerLabel.text=timeString(timeCount)
timer=NSTimer.scheduledTimerWithTimeInterval(时间间隔,目标:self,选择器:#选择器(TimerViewController.TimerDend(35;:)),用户信息:nil,重复:true)
schedulePushNotification()
registerBackgroundTask()
}
}
@iAction func resetTimerButtonTapped(发送方:UIButton){
timer.invalidate()
resetTimeCount()
timerLabel.text=timeString(timeCount)
cancelAllNotifications()
}
//马克:定时器
func resetTimeCount(){
timeCount=timerEnd
}
func TIMERDINDED(计时器:NSTimer){
//倒计时器
timeCount=timeCount-timeInterval
if timeCount字符串{
让分钟=整数(时间)/60
秒=时间-双倍(分钟)*60
设秒分数=秒-双精度(整数(秒))
返回字符串(格式:“%02i:%02i.%02i”,分钟,整数(秒),整数(秒分数*100.0))
}
//马克:背景任务
func registerBackgroundTask(){
backgroundTask=UIApplication.sharedApplication()。beginBackgroundTaskWithExpirationHandler{
[无主的自己]在
self.endBackgroundTask()
}
断言(backgroundTask!=UIBackgroundTaskInvalid)
}
func backgroundtask(){
如果timeCount!=0.0&&(backgroundTask==UIBackgroundTaskInvalid){
registerBackgroundTask()
}
}
func endBackgroundTask(){
UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
backgroundTask=UIBackgroundTaskInvalid
}
//标记:通知
func schedulePushNotification(){
let notification=UILocalNotification()
notification.alertAction=“返回应用程序”
notification.alertBody=“90秒计时器已完成!”
notification.fireDate=NSDate(timeIntervalSinceNow:timerEnd+1)
UIApplication.sharedApplication().scheduleLocalNotification(通知)
}
func cancelAllNotifications(){
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
}
我放弃了后台任务,并将其替换为存储在NSUserDefaults
中。漂移消失了。以下是最终代码:
class TimerViewController: UIViewController {
@IBOutlet weak var startTimerButton: UIButton!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var resetTimerButton: UIButton!
var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.01
let timerEnd:NSTimeInterval = 90
var timeCount:NSTimeInterval = 0
override func viewDidLoad() {
super.viewDidLoad()
resetTimeCount()
timerLabel.text = timeString(timeCount)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TimerViewController.applicationWillResignActive),name: UIApplicationWillResignActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TimerViewController.applicationDidBecomeActive),name: UIApplicationDidBecomeActiveNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
@IBAction func startTimerButtonTapped(sender: UIButton) {
if !timer.valid { //prevent more than one timer on the thread
timerLabel.text = timeString(timeCount)
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self,selector: #selector(TimerViewController.timerDidEnd(_:)),userInfo: nil, repeats: true)
schedulePushNotification()
}
}
@IBAction func resetTimerButtonTapped(sender: UIButton) {
timer.invalidate()
resetTimeCount()
timerLabel.text = timeString(timeCount)
cancelAllNotifications()
}
// MARK: Timer
func resetTimeCount(){
timeCount = timerEnd
}
func timerDidEnd(timer: NSTimer){
//timer that counts down
timeCount = timeCount - timeInterval
if timeCount <= 0 { //test for target time reached.
timerLabel.text = "Time is up!!"
timer.invalidate()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
cancelAllNotifications()
} else { //update the time on the clock if not reached
timerLabel.text = timeString(timeCount)
}
}
func timeString(time: NSTimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%02i",minutes,Int(seconds),Int(secondsFraction * 100.0))
}
// MARK: Timer Storage
struct PropertyKey {
static let timeCountKey = "TimerViewController_timeCount"
static let timeMeasurementKey = "TimerViewController_timeMeasurement"
}
dynamic private func applicationWillResignActive() {
if !timer.valid {
clearDefaults()
NSLog("Clearning defaults")
} else {
saveDefaults()
NSLog("Saving defaults")
}
}
dynamic private func applicationDidBecomeActive() {
if timer.valid {
loadDefaults()
NSLog("Loading defaults")
}
}
private func saveDefaults() {
let userDefault = NSUserDefaults.standardUserDefaults()
userDefault.setObject(timeCount, forKey: PropertyKey.timeCountKey)
userDefault.setObject(NSDate().timeIntervalSince1970, forKey: PropertyKey.timeMeasurementKey)
userDefault.synchronize()
}
private func clearDefaults() {
let userDefault = NSUserDefaults.standardUserDefaults()
userDefault.removeObjectForKey(PropertyKey.timeCountKey)
userDefault.removeObjectForKey(PropertyKey.timeMeasurementKey)
userDefault.synchronize()
}
private func loadDefaults() {
let userDefault = NSUserDefaults.standardUserDefaults()
let restoredTimeCount = userDefault.objectForKey(PropertyKey.timeCountKey) as! Double
let restoredTimeMeasurement = userDefault.objectForKey(PropertyKey.timeMeasurementKey) as! Double
let timeDelta = NSDate().timeIntervalSince1970 - restoredTimeMeasurement
print(timeDelta)
print(timeCount - restoredTimeCount - timeDelta)
timeCount = restoredTimeCount - timeDelta
}
// MARK: Notifications
func schedulePushNotification() {
let notification = UILocalNotification()
notification.alertAction = "Go back to App"
notification.alertBody = "The 90s timer is finished!"
notification.fireDate = NSDate(timeIntervalSinceNow: timerEnd+1)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func cancelAllNotifications() {
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
}
class TimerViewController:UIViewController{
@IBVAR弱启动按钮:UIButton!
@IBVAR时间标签:UILabel!
@IBOutlet弱var重置时间按钮:UIButton!
var timer=NSTimer()
让时间间隔为:NSTimeInterval=0.01
让timerEnd:NSTimeInterval=90
变量时间计数:NSTimeInterval=0
重写func viewDidLoad(){
super.viewDidLoad()
resetTimeCount()
timerLabel.text=timeString(timeCount)
NSNotificationCenter.defaultCenter().addObserver(self,selector:#selector(TimerViewController.applicationWillResignActive),名称:UIApplicationWillResignActivification,对象:nil)
NSNotificationCenter.defaultCenter().addObserver(self,选择器:#选择器(TimerViewController.ApplicationIDBecMeactive),名称:UIApplicationIDBecMeactiveNotification,对象:nil)
}
脱硝{
NSNotificationCenter.defaultCenter().removeObserver(自)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//马克:行动
@iAction func startTimerButtonTapped(发送方:UIButton){
if!timer.valid{//防止线程上有多个计时器
timerLabel.text=timeString(timeCount)
timer=NSTimer.scheduledTimerWithTimeInterval(时间间隔,目标:self,选择器:#选择器(TimerViewController.TimerDend(35;:)),用户信息:nil,重复:true)
schedulePushNotification()
}
}
@iAction func resetTimerButtonTapped(发送方:UIButton){
timer.invalidate()
resetTimeCount()
timerLabel.text=timeString(timeCount)
cancelAllNotifications()
}
//马克:定时器
func resetTimeCount(){
timeCount=timerEnd
}
func TIMERDINDED(计时器:NSTimer){
//倒计时器
timeCount=timeCount-timeInterval
如果时间