好。查看了可能的答案,但在这里看不到我的问题。
我有一个沼泽标准的GCD重复计时器:
class RepeatingGCDTimer {
/// This holds our current run state.
private var state: _State = ._suspended
/// This is the time between fires, in seconds.
let timeInterval: TimeInterval
/// This is the callback event handler we registered.
var eventHandler: (() -> Void)?
/* ############################################################## */
/**
This calculated property will create a new timer that repeats.
It uses the current queue.
*/
private lazy var timer: DispatchSourceTimer = {
let t = DispatchSource.makeTimerSource() // We make a generic, default timer source. No frou-frou.
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval) // We tell it to repeat at our interval.
t.setEventHandler(handler: { [unowned self] in // This is the callback.
self.eventHandler?() // This just calls the event handler we registered.
})
return t
}()
/// This is used to hold state flags for internal use.
private enum _State {
/// The timer is currently paused.
case _suspended
/// The timer has been resumed, and is firing.
case _resumed
}
/* ############################################################## */
/**
Default constructor
- parameter timeInterval: The time (in seconds) between fires.
*/
init(timeInterval inTimeInterval: TimeInterval) {
self.timeInterval = inTimeInterval
}
/* ############################################################## */
/**
If the timer is not currently running, we resume. If running, nothing happens.
*/
func resume() {
if self.state == ._resumed {
return
}
self.state = ._resumed
self.timer.resume()
}
/* ############################################################## */
/**
If the timer is currently running, we suspend. If not running, nothing happens.
*/
func suspend() {
if self.state == ._suspended {
return
}
self.state = ._suspended
self.timer.suspend()
}
/* ############################################################## */
/**
We have to carefully dismantle this, as we can end up with crashes if we don't clean up properly.
*/
deinit {
self.timer.setEventHandler {}
self.timer.cancel()
self.resume() // You need to call resume after canceling. I guess it lets the queue clean up.
self.eventHandler = nil
}
}
太棒了!
...除非不是。
那是当我将设备置于飞行模式时。
此时,计时器停止触发。
即使我退出飞行模式,计时器也不会重新启动。
该应用程序使用UIApplication.shared.isIdleTimerDisabled = true / false来使应用程序保持唤醒状态,但这似乎并不能阻止事件的发生。
有人能帮助我了解这里发生的事情以及如何解决该问题吗?
此应用需要在飞行模式下工作。实际上,它最有可能在飞行模式下使用。
答案 0 :(得分:1)
好。我想我解决了这个问题。像这些东西一样,通常是PEBCAK。
我有一个例程,可以在应用程序后台运行时停止计时器,而在应用程序前台运行时无法进行相应的重启。
当我向上滑动控制中心时,它将使应用程序后台运行。
我不好。
是的,这很尴尬,但是我想在此保留这个问题,以警告其他人。