Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This isn't too complicated. You just need to hang on to the first notification until either the timer fires or the second notification comes in. The comments I put in the code should explain the procedure.</p> <pre><code>- (void)awakeFromNib { // Register for the notification you're interested in [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesNoteCallback:) name:NSTheiTunesNotificationImInterestedIn object:nil]; // @"iTunes"? } </code></pre> <hr> <pre><code>- (void)iTunesNoteCallback:(NSNotification *)note { // Check whether there's been a notification already if( !gotFirstNote ){ // If so, hang on to it, gotFirstNote = YES; self.currNote = note; // With currNote declared as a retained property // and start a timer. noteTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(actOniTunesInfo:) userInfo:nil repeats:NO]; } else { // However, if we got a notification already, // hang on to the new one, self.currNote = note; // stop the timer, [noteTimer invalidate]; // and call the same method the timer would have [self actOniTunesInfo:nil]; } } </code></pre> <hr> <pre><code>- (void)actOniTunesInfo:(NSTimer *)timer { // Reset the flag gotFirstNote = NO; // Use currNote; doesn't matter which one it is, // it's the best info we've gotten } </code></pre> <p>If there are two <em>types</em> of notifications, it's even simpler. You no longer need the flag, you just register two callbacks, and as soon as the second one is called, you can invalidate the timer, release the first notification, and use the info you've just gotten.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload