我继承了一些用于检测结束语的代码。
...
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
if result != nil {
if self.eosTimer != nil {
self.eosTimer!.invalidate()
self.eosTimer = nil
}
self.eosTimer = Timer(timeInterval: 2.0, target: self, selector: #selector(self.timerHasCompleted(timer:)), userInfo: nil, repeats: false)
RunLoop.current.add(self.eosTimer!, forMode: .commonModes)
}
...
我认为这是因为计时器被添加到RunLoop。没有这样做,计时器根本不会发射。如何让eosTimer只触发一次?