I inherited some code for use in detection of end of speech.
...
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)
}
...
I assume this is because the timer is added to RunLoop. Without doing that, the timer doesn't fire at all. How can I get the eosTimer to only fire once?