NSNotificationCenter vs delegation( using protocols )?

20,809

Solution 1

The rule of thumb here is how many clients would like to be notified of an event. If it's mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model.

If the event you emit may be of an interest to many objects at once (e.g. screen rotated, memory usage, user login/logout), then you should use the NSNotificationCenter.

Solution 2

Their purposes are different:

  • Notification is used to broadcast messages to possibly several recipients unknown from the sender.

  • Delegation is used to send messages to a single known recipient acting on behalf of the sender.

Solution 3

Notifications are generally better for notifying the UI of changes the occur on other threads as well. Apple's documentation strongly discourages the use of delegates across threads where possible, both for stability and performance reasons. On the Mac, they suggest using Bindings, but since they don't exist on the iPhone, notifications are probably your next best bet.

Solution 4

Considering the performance is a good idea (delegation better for small number of notified objects, notification centre better for larger number of objects, or is it? run a profiler) but I think a more important factor since you are talking about Objective-C and less likely to be talking about the really high performance parts of your code base, which are likely to be written in C, is reducing compile-time dependencies between modules.

There is nothing to stop you having an array of delegates rather than a single delegate.

I might use NSNotificationCenter only for status of any network stack components I make and any custom device status monitoring interfaces. But for most coupling, not to do with global status of the app, I think it is clearer to use normal interface contracts in Objective-C in most cases and easier to folow for the people coming after you than to use NSNotificationCenter. In fact I have never used NotificationCenter for my own custom events and prefer to use delegates for ease of code comprehension by someone else reading my code.

And finally, of course with notifications to/from the standard API you don't have choice and must use whichever of the two methods Apple proscribe for a given event.

Solution 5

Notifications are better for decoupling UI components. It allows you to plug any view without any modification in your controllers or models. Definitely better for loosely-coupled design.

But for the performance between delegation and notification, you need to think about the frequency of the call.

Delegation might be better for more frequent events, notifications are better for less frequent events but more recipients. It's up to project what to pick.

Share:
20,809
EEE
Author by

EEE

Updated on February 12, 2020

Comments

  • EEE
    EEE over 4 years

    What are the pros and cons of each of them?
    Where should I use them specifically?

  • benzado
    benzado over 14 years
    I'd also add that notifications are one-way (e.g., DidFireMissle), whereas a delegate is required if you need to return information (e.g., -(BOOL)shouldFireMissle).
  • Glenn Maynard
    Glenn Maynard almost 12 years
    @benzado: Actually, you can pass an object along with a message, which receivers can change. You need to keep in mind that multiple (or zero) receivers might handle the notification, but it's valid.