NSNotificationCenter vs delegation( using protocols )?

IphoneObjective CDelegatesProtocolsNsnotifications

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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 - Iphone

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 - Iphone

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 - Iphone

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 - Iphone

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.

Solution 6 - Iphone

An option in between those two is using the observer pattern, without NSNotificationCenter. Look at my Objective-C implementation here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEEEView Question on Stackoverflow
Solution 1 - IphonenotnoopView Answer on Stackoverflow
Solution 2 - IphonemouvicielView Answer on Stackoverflow
Solution 3 - IphoneShawn CraverView Answer on Stackoverflow
Solution 4 - IphonemartinrView Answer on Stackoverflow
Solution 5 - IphoneCOzkurtView Answer on Stackoverflow
Solution 6 - IphoneAlejandroView Answer on Stackoverflow