gpt4 book ai didi

swift - 如何防止短背景计时器漂移?

转载 作者:可可西里 更新时间:2023-11-01 02:16:54 26 4
gpt4 key购买 nike

我有一个 90 年代计时器,它在后台运行,通过注册 beginBackgroundTaskWithExpirationHandler 完成。这看起来很简单,但我遇到的问题是计时器本身漂移很大。对于 90 秒计时器,我得到大约 30-35 秒的漂移。即,如果我启动计时器,让应用进入后台,然后在 90 秒后打开应用,计时器会显示剩余 30 秒。

如果我让应用程序在整个 90 年代都处于打开状态,我将获得零漂移。如果我将计时器的时间间隔降低到 1 秒(而不是我首选的 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()
}

}

最佳答案

我放弃了后台任务并将其替换为 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()
}

}

关于swift - 如何防止短背景计时器漂移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com