Why is my Timer selector firing twice (RunLoop)?

1k Views Asked by At

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?

0

There are 0 best solutions below