MacOS SwiftUI App Update variables at midnight

Hi,

I'm new here. I have an app that I want to refresh a few variables at midnight. What ways can i go about doing this?

I have an AppDelegate started, but I'm not sure how to make it work, yet.

Is there an easy way to make it update?

Answered by DTS Engineer in 814829022

Ah, so the Mac then [1]. I was hoping you’d be targeting iOS because UIKit has the significantTimeChangeNotification notification, which does exactly what you want.

The infrastructure used by that notification is present on macOS, but it’s not API )-: That means you’ll need to reimplement bits of it. There are three parts to that:

Setting up a timer that fires at midnight is surprisingly hard to do correctly. To start, you should tear down the timer and reschedule it every time you get one of the above notifications. That’s the easy part.

The hard part is making sure it fires at midnight. Much of our timer infrastructure is based on Mach absolute time, so the clock stop to tick when the CPU stops. That means you need to cancel and reschedule your timer when coming back from a CPU sleep. It’s possible to do that, but the code isn’t trivial.

An easier option is to set a timer to fire every minute. It can check to see if the day has changed and, if so, trigger a UI update. This is essentially polling, but polling every minute is cheap enough that it’s not something I’d worry about.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] D’oh! And there it was in the thread title, I just missed it )-:

I want to refresh a few variables at midnight.

What platform are you targeting?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I wanted to keep the App for at least Ventura.

Is it possible to do?

I couldn't go higher than Sonoma 14.5. For sure.

Accepted Answer

Ah, so the Mac then [1]. I was hoping you’d be targeting iOS because UIKit has the significantTimeChangeNotification notification, which does exactly what you want.

The infrastructure used by that notification is present on macOS, but it’s not API )-: That means you’ll need to reimplement bits of it. There are three parts to that:

Setting up a timer that fires at midnight is surprisingly hard to do correctly. To start, you should tear down the timer and reschedule it every time you get one of the above notifications. That’s the easy part.

The hard part is making sure it fires at midnight. Much of our timer infrastructure is based on Mach absolute time, so the clock stop to tick when the CPU stops. That means you need to cancel and reschedule your timer when coming back from a CPU sleep. It’s possible to do that, but the code isn’t trivial.

An easier option is to set a timer to fire every minute. It can check to see if the day has changed and, if so, trigger a UI update. This is essentially polling, but polling every minute is cheap enough that it’s not something I’d worry about.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] D’oh! And there it was in the thread title, I just missed it )-:

MacOS SwiftUI App Update variables at midnight
 
 
Q