NSNotificationCenter vs delegation( using protocols )?
Asked Answered
C

6

70

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

Cyte answered 18/12, 2009 at 12:33 Comment(0)
M
105

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.

Millsaps answered 18/12, 2009 at 12:41 Comment(2)
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).Structure
@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.Iconolatry
I
40

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.

Imperturbation answered 18/12, 2009 at 12:39 Comment(0)
Q
12

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.

Quamash answered 18/12, 2009 at 13:40 Comment(0)
K
7

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.

Kerri answered 18/12, 2009 at 12:59 Comment(0)
G
6

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.

Gonnella answered 15/7, 2011 at 17:2 Comment(0)
S
3

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

Severity answered 13/10, 2010 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.