Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use NSNotificationCenter

I want to have multiple observers on multiple events of a single object (1-to-N relationship).

A mechanism to achieve this task is provided by the NSNotificationCenter. The mechanism looks pretty overkill when used for my problem.

How I would do it manually without the use of NSNotificationCenter:

- (void)addDelegate:(id<DelegateProtocol>)delegate;
- (void)removeDelegate:(id<DelegateProtocol>)delegate;

to add and remove observers from my object.

- (void)someEventFired:(NSObject<NSCopying> *)eventData
{
    for (id delegate in delegates) {
        NSObject *data = [eventData copy];
        [delegate someEventFired:data];
    }
}

This mechanism is straight-forward and simple to implement without the objects having to share additional strings.

  • Is there an official pattern for 1-to-N delegates (like C# events) in an iOS framework besides the NSNotificationCenter?
  • When should the NSNotificationCenter be used and when not?
  • When should an implementation like the one I am suggesting here be used and when not?
like image 381
Etan Avatar asked May 31 '12 15:05

Etan


2 Answers

By convention, delegates should probably only be used for 1:1 relationships. If you really need 1:N relationships for this type of functionality, you have two options:

  1. As you mentioned, NSNotificationCenter.
  2. Key-Value Observing (also known as KVO).

KVO is appropriate if you only care about when a particular property of an object changes. Otherwise, you should really just consider using NSNotificationCenter. You can even be notified only when a specific object posts that notification by passing that object into the addObserver:selector:name:object: method.

Apple uses NSNotification in similar scenarios (like the notifications defined for UITextField, including UITextFieldTextDidBeginEditingNotification, UITextFieldTextDidChangeNotification, and UITextFieldTextDidEndEditingNotification).

like image 101
Sebastian Celis Avatar answered Sep 20 '22 22:09

Sebastian Celis


using notifications is broadcasting: 1 sender just sends an information and who ever tuned in, receives it. Petty much like a radio station, there is no channel back (lets for the moment forget about telephones)

delegation is something different. Th object, that asks a deleagte to do something, usually needs a result of that request, there fore delegation is a 1-to-1 communication, that is always initiated by the object, not the delegate (while the object can have methods that can be called to inform the object to initiate the communication, ie [tableView reloadData]).

So if the sender needs to get data back, it is delegation. If the sender doesn't care about anything after broadcasting, go with notifications.

If you run into the situation, that you need delegation, but several objects should implement the protocol. you should have 1 delegate, that hold references to the other objects and calls the methods on the senders behalf — or you could go with blocks.

like image 40
vikingosegundo Avatar answered Sep 21 '22 22:09

vikingosegundo