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?