Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abusing NSNotificationCenter

NSNotificationCenter is really cool and we can build very extensible apps with it. My question is: Can we abuse using NSNotificationCenter in an application i.e. use it intensively or is it not really a good choice? Currently I am using it when I have JSON services responses: I post notifications and so I have several listeners listening to this type of notification. So far I have almost tens of services to call and let's say many more actions to achieve based on these notifications. WDYT? Thanks

like image 508
Christophe HAMERLING Avatar asked Nov 16 '11 20:11

Christophe HAMERLING


2 Answers

In my experience writing an application that makes extensive use of the NSNotificationCenter causes a maintenance nightmare, requiring references to the (delegate)objects that use your data means that you can step through the actual process of dealing with your data; with notifications things are just too disconnected.

That said, there are many times in which a Notification makes sense (device rotation, app startup/exit, broad state changes), but I usually try to avoid them when I can.

One place that I have found Notifications to work well are when I have a controller that manages data that is used by many other controllers; when the data changes I kick off a notification, this allows all interested parties to update. For example, I may have a UserController that manages information about the current user (name, photo, etc...). When some piece of user data is changed I'll post a notification (e.g. UserControllerDidUpdatePhotoNotification). Any interested view controllers could then choose to update the photo that they were displaying.

I suppose that the biggest reason preventing me from using notifications is a desire for maintainability, if your use of notifications still allows you to have maintainable code then keep using them; if not, switch to some other design.

like image 65
jessecurry Avatar answered Oct 17 '22 23:10

jessecurry


When NSNotificationCenter sends a notification it really isn't any different from having the notification sender message the recipient directly. The benefit that NSNotificationCenter provides is the ability for objects to communicate without them having to maintain references to each other.

I think the trick to not abusing NSNotificationCenter is to make sure that your notification handlers are efficient. If you know that lots of objects are going to be consuming a single notification then you should be darn sure that these notification handlers are as efficient as possible. There are tricks you can do to do this, such as populating the NSNotification's userinfo dictionary with valuable information that can be used by the notification handlers to easily and quickly determine what they should do (if anything) with the notification.

You can use Instruments to measure how long your notification handlers are running and from that you can try to optimize things further. Remember that notifications handlers are called on the same thread that was responsible for generating the notification, so all notification handlers run one at a time until they are all done. Be efficient!

like image 37
Carter Avatar answered Oct 18 '22 00:10

Carter