• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSNotification filtering problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNotification filtering problem


  • Subject: Re: NSNotification filtering problem
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Mon, 8 Mar 2004 03:39:33 -0800

Hello...

Assuming you don't need information that is provided along with the notification (ie the object or the userInfo dictionary), an easy solution is to do something like this:

/* typed in email, etc... */

- (void)theMethodUsedWhenRegisteringAsObserverForNotification:(NSNotification *)note
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleThisParticularNotification) object:nil];
[self performSelector:@selector(handleThatParticularNotification) withObject:nil afterDelay:0.05];
}

- (void)handleThisParticularNotification
{

/* do your task here... */

}

You would want to adjust the delay used, depending on what sort of "very short time" you are working with. It should represent the maximum allowed amount of time between each of the many notifications before the handler is called, not the total amount of time it takes for all of the notifications to occur. In other words, for a delay of 0.05 as above, the handler will only be called after 0.05 seconds have passed with no additional notifications being posted -- if another notification is posted that triggers the first method, the delay will be reset to 0.05 seconds again.


If you need access to some information in the notification (the object or userInfo) when your handler runs, then it can become a bit more complicated, since you would have to pass that information as the withObject: argument of the performSelector:withObject:afterDelay: message. This affects how cancelPreviousPerformRequestsWithTarget:selector:object: works, since that object is tested using isEqual: to determine if the request matches any of the previous perform requests. That may or may not be a good thing, depending on what you are trying to do. In the case where you just need the object of the notification, with an adequate delay the default behavior works well since a series of notifications involving multiple objects could be coallesced into a group of calls to the handler method, with just one call for each object referred to in the series notification.


Hope that helps,

Louis


Hi list !

i have a little problem with NSNotification RECEIVING :

a piece of code which *i don t have access to source* is used in my program and post me a notification called "myNotification".
The problem is that this notification is sent about 100 times in a very short time (probably a misconception of the author, or my misconception ? ;) ). This results in useless call to my notification handler since i need only one posting of this notification...

I was really happy to see about notificationQueues and their coalescing capabilities... but AFAIK NSNotificationQueues are used on the *sender* side, they can't be used on the receiver side ?!

Does anybody has a tip to avoid multiple successive notification handler call on the receiver side ? a sort of filter (like NSNotificationQueues coalescing techniques) but on the NotificationCenter side...

Thanks !

Guillaume
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >NSNotification filtering problem (From: Guillaume Rager <email@hidden>)

  • Prev by Date: NSArrayController and
  • Next by Date: Re: NSEditor, NSEditorRegistration Protocols
  • Previous by thread: NSNotification filtering problem
  • Next by thread: NSArrayController and
  • Index(es):
    • Date
    • Thread