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?
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:
-
Detecting time zone changes. Do that using the
NSSystemTimeZoneDidChange
notification. -
Detecting clock changes. Do that using the
NSSystemClockDidChange
notification. -
Setting up a timer that fires at midnight. See below.
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 )-: