DispatchSourceTimer 的setEventHandler函数不执行

前提:sourceTimer已经设置为全局变量,sourceTimer在创建之前也调用了cancel(),创建之后调用过resume()。

func startTimer() {

        self.sourceTimer?.cancel()
        let queue = DispatchQueue(label: "\(AppDomain).verticalCycle.timer", attributes: .concurrent)
        sourceTimer = DispatchSource.makeTimerSource(flags: .strict, queue: queue)
        sourceTimer?.schedule(deadline: .now(), repeating: duration, leeway: .milliseconds(100))
        sourceTimer?.setEventHandler(handler: {
            DispatchQueue.main.async {
               
            }
            
        })
        sourceTimer?.resume()
    }

暂时的解决办法:调用定时器的创建开启时给0.1S的延时

func openTimer() {
        cancelTimer()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.startTimer()
        }
        
    }

你可能感兴趣的:(DispatchSourceTimer 的setEventHandler函数不执行)