Which is the best way to repeat a code every 12 hours in swift for Mac OSX application

Below are the two ways i am doing it, but wanted to know which is best way to do it and why


First way


Timer.scheduledTimer(withTimeInterval: 43200, repeats: true) { timer in
            let randomNumber = Int.random(in: 1...20)
            print("Number: \(randomNumber)")

            if randomNumber == 20 {
                timer.invalidate()
            }
        }



second way



let date = Date().addingTimeInterval(43200)
        let timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(runCode), userInfo: nil, repeats: false)
        RunLoop.main.add(timer, forMode: .common)
@objc func runCode()
           {
               print("Running code after every 5 seconds")
           }
Answered by DTS Engineer in 406033022

use

NSBackgroundActivityScheduler
and none of the above mentioned ways.

Correct. Doing this with a timer is quite tricky because timers behave strangely when the CPU sleeps. Also, your timer is not coordinated with other maintenance activity.

NSBackgroundActivityScheduler
takes care of all of this for you.

Be aware that part of the

NSBackgroundActivityScheduler
API contract is that it won’t run your code at exactly the time you request. Rather, it will hit this deadline on average. It’s this flexibility that allows it to coordinate your work with other maintenance work.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

'best' can depend on what your app does and what it does with this snippet - perhaps if you discussed those, someone could help point you in the right direction....or is this just a homework assignment and you're trying to learn....?

Like every 12 hours i am trying to download a file from server and sync it with my local file, thats what my code snippet does

The droid you’re looking for here is

NSBackgroundActivityScheduler
. You can tell it that you wan to run roughly every 12 hours and it’ll call you back at the right time to do your work, coordinating it with other maintenance activity on the system.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

So you are suggesting me to use NSBackgroundActivityScheduler and none of the above mentioned ways.

Accepted Answer

use

NSBackgroundActivityScheduler
and none of the above mentioned ways.

Correct. Doing this with a timer is quite tricky because timers behave strangely when the CPU sleeps. Also, your timer is not coordinated with other maintenance activity.

NSBackgroundActivityScheduler
takes care of all of this for you.

Be aware that part of the

NSBackgroundActivityScheduler
API contract is that it won’t run your code at exactly the time you request. Rather, it will hit this deadline on average. It’s this flexibility that allows it to coordinate your work with other maintenance work.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Which is the best way to repeat a code every 12 hours in swift for Mac OSX application
 
 
Q