In my iOS application, I need to do an action whenever there is no result coming in for 2 seconds.
So I set a timer at the beginning:
var timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(ViewController.noResults), userInfo: nil, repeats:false)
and whenever I get results, I destroy the timer and start a new timer object. I check if timer is valid to know if 2 seconds has been reached. When I get no results for 2 seconds, I do some action:
if result != nil {
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval:2, target: self, selector: #selector(ViewController.noResults), userInfo: nil, repeats:false)
}
if timer.isValid {
print("has not been 2 seconds yet")
} else {
print("do some action")
}
This isn't working and I never get to the do some action clause. Can someone point me to the right way?