Do something at a specific time

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?

You could try dispatch_after (using the main queue). However... none of the methods will ever be exact. A sleeping computer, for example, generally won't be running code in the background. And the OS scheduler is, ahem, complicated, and the load on the system can impact that.

Do something at a specific time
 
 
Q