I am using the following code to invoke a function periodically every second. The problem is delay is actually 1.1 seconds and gets drifted more and more eventually as can be seen in NSLogs (and it is visible in other parts of the code as well apart from NSLog). Am I doing it wrong, or should I be using timer?
private func updateTimeCode() {
NSLog("Updating time")
//Some more code that doesn't take time
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
[weak self] in
self ? .updateTimeCode()
}
}
019-08-06 17:15:19.713234+0530 MyApp-Swift[8299:2685215] Updating time 2019-08-06 17:15:20.812652+0530 MyApp-Swift[8299:2685215] Updating time 2019-08-06 17:15:21.913188+0530 MyApp-Swift[8299:2685215] Updating time 2019-08-06 17:15:23.028814+0530 MyApp-Swift[8299:2685215] Updating time