NSSystemClockDidChangeNotification 在应用程序唤醒时表现得很奇怪

NSSystemClockDidChangeNotification acts weird when app wakes up

在我的应用程序中,我在 AppDelegate 方法 application:application didFinishLaunchingWithOptions:options 中注册了一个 NSSystemClockDidChangeNotification 通知。

Posted whenever the system clock is changed. This can be initiated by a call to settimeofday() or the user changing values in the Date and Time Preference panel. The notification object is null. This notification does not contain a userInfo dictionary.

一个月前还好好的,但是这几天每次我挂起我的应用程序,锁定我的iPhone并留给它2分钟通过,当打开应用程序时,选择器方法被调用,这是对我来说很奇怪。我没有更改系统或时间,只是让设备闲置。

谁能帮我理解一下?我只想在用户手动更改系统时间时执行一些代码,就在这种情况下(尝试使用 UIApplicationSignificantTimeChangeNotification 但没有帮助)。

像那样:Getting iOS system uptime, that doesn't pause when asleep

但请记住,当无法连接到 Internet 时,当系统尝试从 NTP 服务器更新时间时,由于不同步,此解决方案有时会失败。

这个回答有点晚了,但这是苹果支持团队在我问类似问题时的回答:

我问的部分问题:

Could it be possible for the OS posted [this notification] if the time/timezone has not changed in the device?

我从他们那里得到的答案:

Absolutely [1]. It's common for a notification like this to be posted redundantly. In some cases it's triggered by a state change you can't see. For example — and I haven't tested this theory, so it's just an example of how this sort of thing can come about — this notification might be posted if the system's giant list of time zone info has changed. So, the time zone state has changed, but it's not something that affects your app.

But in other cases, a notification might be truly redundant (-: iOS is a complex system and in some cases this complex machinery generates redundant notifications, and there's no need to filter them out because…

Your app should respond to such notifications be refetching the info it cares about and updating its state based on that. If this update is expensive, keep your own copy of the previous state, compare the current system state to that, and only do the update if the stuff you care about has actually changed.