快速计算在后台运行的计时器的时间 [英] swift calculate time for timers running in background
问题描述
我想运行2个可互换的计时器,但是即使应用程序在后台,我也希望它们运行".
I want to run 2 interchangeable timers but I want them to "run" even if the app is in the background.
理想的行为是,用户启动计时器,进入后台,并且仅等待可互换计时器触发的通知...计时器可以互换很多次.当用户将应用程序重新显示到前台时,他希望查看当前计时器的准确时间.
The desirable behavior is, that user starts the timer, goes background and just waits for notifications for the interchangeable timers to trigger ... the timers can interchange a lot of times. When the user brings the app back to foreground .. he wants to see the accurate time of the current timer.
在iOS模拟器中,计时器可以在后台工作,但不能在设备上工作...
Timers do work in background when in iOS Simulator, but not on a device ...
根据我附上的图片,我试图计算X当前计时器应到期的时间,并根据结束时间获取当前时间.代码如下:
According to the picture I attached, I am trying to calculate the X The time when the current timer should expire and acquire the current time according to the end time. The code follows:
func appDidBecomeActive(){
// TODO: calculate time spend in background
isInBackground = false
if let startTime = startTime{
let now = NSDate()
var timeFromBegining = now.timeIntervalSinceDate(startTime)
while timeFromBegining > userDefaultsHelper.timerAseconds(){
timeFromBegining -= userDefaultsHelper.timerAseconds()
timerType = .tiemrTypeB
let stopTimeSeconds = userDefaultsHelper.timerBseconds() - timeFromBegining
stopTime = NSDate().dateByAddingTimeInterval(stopTimeSeconds)
print("Time from Begining: \(timeFromBegining)")
if timeFromBegining > userDefaultsHelper.timerBseconds(){
timeFromBegining -= userDefaultsHelper.timerBseconds()
timerType = .timerTypeA
// TODO: do something with the remaining time ...
let stopTimeSeconds = userDefaultsHelper.timerAseconds() - timeFromBegining
stopTime = NSDate().dateByAddingTimeInterval(stopTimeSeconds)
print("Time from Begining: \(timeFromBegining)")
}
}
}
}
推荐答案
我个人认为A和B的组合持续时间为一个周期".然后,当应用程序重新启动时,我将进行模数计算以确定(a)自首次启动计时器A以来已经过去了多少个周期; (b)计算我们在当前周期内的位置.据此,您可以轻松地计算出每个定时器下次出现之前还剩下的时间:
Personally, I'd consider the combined duration of A and B to be one "cycle". And then, when the app restarts, I'll do the modulus calculations to determine (a) how many cycles have past since timer A was started the first time; and (b) calculate where we are within the current cycle. From that you can easily calculate the amount of time left until the next occurrence of each timer:
let elapsed = Date().timeIntervalSince(startTime)
let totalDuration = durationOfTimerA + durationOfTimerB
let whereInCycle = elapsed.remainder(dividingBy: totalDuration)
let cycleCount = Int(elapsed / totalDuration)
var timeUntilNextA: Double
var timeUntilNextB: Double
if whereInCycle < durationOfTimerA {
timeUntilNextA = durationOfTimerA - whereInCycle
timeUntilNextB = timeUntilNextA + durationOfTimerB
} else {
timeUntilNextB = durationOfTimerA + durationOfTimerB - whereInCycle
timeUntilNextA = timeUntilNextB + durationOfTimerA
}
if cycleCount < 1 {
if whereInCycle < durationOfTimerA {
print("timer A hasn't fired yet")
} else {
print("timer A has fired, but B hasn't")
}
} else {
if whereInCycle < durationOfTimerA {
print("both timers A and B have fired \(cycleCount) times, and waiting for A to fire next")
} else {
print("timers A has fired \(cycleCount+1) times, but B has fired only \(cycleCount) times; we waiting for B to fire next")
}
}
print("A will fire in \(timeUntilNextA) seconds; B will fire in \(timeUntilNextB)")
这篇关于快速计算在后台运行的计时器的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!