NSNotification, when does the day change?

The WebKit WebHistory API separates its positions by date. So when the day changes, I need to reassign the labels "Yesterday" and / or "(earlier) today" (or "Tomorrow !!"). Is there an NSNotification for this? Or should I rely on NSTimer or -performSelector:withObject:andDelay:

or some other method?

(Of course, alternative methods don't automatically handle feedback.)

Adding

At the time of this writing, there are two replies, but there was also a third that was later deleted. He had the most complete answer. He referred to WWDC-2013 Session 227, which discusses undocumented additions to the Date / Time system in Mavericks. Improved Today check and change of day notification. You can check two-thirds down the major releases for OS X version 10.9 .

(If the owner of the remote response wants to return something, I'll pass the response flag if possible.) I got my Mac in late 2013 and immediately upgraded it to Mavericks, so I don't care, -Mavericks and the Mavericks APIs are fine.

+3


source to share


2 answers


Is there an NSNotification for this?

No, Mac OS does not send notifications when the day changes.

when the day changes, I need to reassign any "Yesterday" tags [...]

It depends on the type of UI displaying the label. In temporary UIs, as in menus, it’s probably best to just compute the shortcut when you open the UI and leave it as it is.



If some user interface displays the label for a longer time, such as a sidebar, it is best to update it when the day changes.

To do this, you must time the next change and set up a timer. In addition, you can register for some notifications to detect non-standard daytime changes (system sleep, system time setting, calendar changes).

Here's the code that does all of this and just sends a notification. It works on both iOS and Mac OS.

+1


source


One way is to set NSTimer

to launch at the start of a new day.

  • Get the start of the current day beginningOfToday

    ( see how to do this here ).
  • Create a timer to start 86400 seconds after beginningOfToday

    (this will be exactly the next day).
  • Use a timer every day if necessary.

To have a timer fire 86400 seconds after the date



[[NSTimer alloc] initWithFireDate:beginningOfToday
                             interval:86400
                               target:`YOUR_TARGET`
                             selector:@selector(`YOUR_SELECTOR`)
                             userInfo:nil
                             repeats:YES];

      

your target could be self

your selector dayDidChange

for example.

-2


source







All Articles