Swift - 带有条件的 GCD 计时器
Swift - Timer with GCD with condition
我使用 Firebase 上传带有进度指示器的文件:
RappleActivityIndicatorView.setProgress(CGFloat(a), textValue: "\(String(a * 100)) %")
print("\(a) %")
我想实现一个条件:如果 % 的值(例如:23%)停留 15 秒或更长时间,它会启动取消上传。
我在想一个 GCD 计时器 :
DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
print("We can launch the cancellation of the upload")
}
但我不知道如何link a 值在 15 秒内未更新的情况。
有什么想法吗?
非常感谢,
试试这个:
var timer: Timer?
// every time you set new percent start new Timer. if in 15 sec it will not reach new percent -> cancellation begins
func startNewTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 15, repeats: false, block: { (_) in
// do cancellation
})
}
一个合适的解决方案是超时计时器。 GCD 计时器的好处是它可以在 运行.
时重新启动
你需要一个属性,定时器参考
var timeoutTimer : DispatchSourceTimer?
然后创建一个启动计时器的方法。如果不是 运行,则创建(一次性)计时器,如果是 运行,则重新启动。在 15 秒后执行的事件处理程序中,打印该行并释放计时器。
func startTimeoutTimer()
{
let delay : DispatchTime = .now() + .seconds(15)
if timeoutTimer == nil {
timeoutTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timeoutTimer!.schedule(deadline: delay, repeating: 0)
timeoutTimer!.setEventHandler {
self.timeoutTimer!.cancel()
self.timeoutTimer = nil
print("We can launch the cancellation of the upload")
}
timeoutTimer!.resume()
} else {
timeoutTimer?.schedule(deadline: delay, repeating: 0)
}
}
要控制计时器,您需要另一个 属性 当前百分比值
var currentValue : CGFloat = 0.0
设置流程后,将值与当前值进行比较,如果值不同,则(重新)启动计时器。如果值相等,则定时器会在延迟 15 秒后触发。如果进度继续,例如 8 秒后计时器再次从零开始。
RappleActivityIndicatorView.setProgress(CGFloat(a), textValue: "\(String(a * 100)) %")
if a != currentValue {
startTimeoutTimer()
currentValue = a
}
并在上传成功完成后删除计时器
self.timeoutTimer!.cancel()
self.timeoutTimer = nil
我使用 Firebase 上传带有进度指示器的文件:
RappleActivityIndicatorView.setProgress(CGFloat(a), textValue: "\(String(a * 100)) %")
print("\(a) %")
我想实现一个条件:如果 % 的值(例如:23%)停留 15 秒或更长时间,它会启动取消上传。
我在想一个 GCD 计时器 :
DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
print("We can launch the cancellation of the upload")
}
但我不知道如何link a 值在 15 秒内未更新的情况。 有什么想法吗?
非常感谢,
试试这个:
var timer: Timer?
// every time you set new percent start new Timer. if in 15 sec it will not reach new percent -> cancellation begins
func startNewTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 15, repeats: false, block: { (_) in
// do cancellation
})
}
一个合适的解决方案是超时计时器。 GCD 计时器的好处是它可以在 运行.
时重新启动你需要一个属性,定时器参考
var timeoutTimer : DispatchSourceTimer?
然后创建一个启动计时器的方法。如果不是 运行,则创建(一次性)计时器,如果是 运行,则重新启动。在 15 秒后执行的事件处理程序中,打印该行并释放计时器。
func startTimeoutTimer()
{
let delay : DispatchTime = .now() + .seconds(15)
if timeoutTimer == nil {
timeoutTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timeoutTimer!.schedule(deadline: delay, repeating: 0)
timeoutTimer!.setEventHandler {
self.timeoutTimer!.cancel()
self.timeoutTimer = nil
print("We can launch the cancellation of the upload")
}
timeoutTimer!.resume()
} else {
timeoutTimer?.schedule(deadline: delay, repeating: 0)
}
}
要控制计时器,您需要另一个 属性 当前百分比值
var currentValue : CGFloat = 0.0
设置流程后,将值与当前值进行比较,如果值不同,则(重新)启动计时器。如果值相等,则定时器会在延迟 15 秒后触发。如果进度继续,例如 8 秒后计时器再次从零开始。
RappleActivityIndicatorView.setProgress(CGFloat(a), textValue: "\(String(a * 100)) %")
if a != currentValue {
startTimeoutTimer()
currentValue = a
}
并在上传成功完成后删除计时器
self.timeoutTimer!.cancel()
self.timeoutTimer = nil