I want my program to do something at a specific time. This is what I have tried:
func rellotge() {
countdownRellotge = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(actualitzaRellotge),
userInfo: nil, repeats: true)
}
@objc func actualitzaRellotge() {
let date = Date()
let calendar = Calendar.current
hour = calendar.component(.hour, from: date)
minutes = calendar.component(.minute, from: date)
seconds = calendar.component(.second, from: date)
//print (minutes, ":", seconds)
if (minutes == 40 && seconds == 0){
print ("do something")
}
}
This does not work if the computer goes to sleep. Even before that, it becomes buggy. (When I print the seconds, it seems that sometimes it skips some seconds)
How can I reliably make my program do something at a specific minute-second?