views:

135

answers:

2

Hello,

there is a better place where an object can be registered to a Notification Center?

In other words, for a matter of performance, it's a good practice to register an object as Notification listener in the init(), awakeFromNib() or other event handler?

Thanks!

+2  A: 

There's not really any performance difference you'll be able to notice by registering for notifications at a different time. The only advice I can give you is to narrow the window that a notification is being observed to as short a duration as possible. Also, make sure you only register an observer once. If you register an observer multiple times, you'll get multiple notifications.

Why are you so concerned about the performance of notifications? Have you confirmed that notifications are causing a problem by measuring?

kperryua
Thanks for your answer! Yes, I've noticed a small difference in loading time if I register all the notifications in the init() method of each object, instead to register them on the awakeFromNib() or other handlers! I've asked that to see if someone had a technical explanation about!
BitDrink
BitDrink: Not quite. The difference is that when you do it in `init`, you do it before your UI has appeared, whereas when you do it in `awakeFromNib`, you do it after (some part of) your UI has appeared. The time taken to add the observer doesn't go away; it just moves to after the UI is up. That's still a good thing, though, and something you should try to do in general.
Peter Hosey
+3  A: 

Do not worry about performance until your code is working and you can observe and measure any performance issues you may have. Registering observers is something done quite infrequently, so there should be no significant performance issues involved in when you do so. What can be significant is how many objects you register for notifications and how many notifications you're sending, since posting notifications involves a hash lookup.

NSResponder