This is a kind of blunder when I used the interval timer in Swift 3.0.
In Swift, If you’d like to use interval timer. like this.
func timerStart() { let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true) RunLoop.main.add(timer, forMode: RunLoopMode.commonModes) } func main() { self.timerStart() } func onTimer { // call every 100ms interval. }
This is Ok.
func timerStart() {
let timer = Timer.scheduledTimer(timeInterval: 0.1,
target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
}
func main() {
//
assetWriter.finishWriting(completionHandler: {
self.timerStart()
}
}
func onTimer {
// call every 100ms interval.
}
But When If you call timer Start in callback function. timerStart not work.
func timerStart() { let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true) RunLoop.main.add(timer, forMode: RunLoopMode.commonModes) } func main() { // assetWriter.finishWriting(completionHandler: { DispatchQueue.main.async { self.timerStart() } } } func onTimer { // call every 100ms interval. }
In case of change to main thread, it’s fine.