TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | Apache 2 |
ReleasedLast Release | Feb 2015 |
Maintained by Alejandro Isaza.
The AIObservable
class lets you to dispatch events to a list of observers. It's an implementation of the Observer design pattern. You can add and remove observers even within the notification callback and from multiple threads or queues. Observers are similar to delegates but you can have multiple of them.
This implementation has a few advantages over NSNotificationCenter
:
removeObserver:
on the observer's dealloc, AIObservable
uses zeroing weak references.collection:didAddObject:atIndex:
as opposed to didAddObject:(NSNotification*)notification
.As an example suppose you have a chat room. The chat room object dispatches obseravable events when messages are added and people join and leave the room.
@class AIMessage;
@class AIUser;
@protocol AIChatRoomObserver;
@interface AIChatRoom : NSObject
- (void)postMessage:(AIMessage*)message;
// ...
- (void)addObserver:(id<AIChatRoomObserver>)observer;
- (void)removeObserver:(id<AIChatRoomObserver>)observer;
@end
@protocol AIChatRoomObserver
@optional
- (void)chatRoom:(AIChatRoom*)room didGetNewMessage:(AIMessage*)message;
- (void)chatRoom:(AIChatRoom*)room userDidJoin:(AIUser*)user;
- (void)chatRoom:(AIChatRoom*)room userDidLeave:(AIUser*)user;
@end
As you can see everything has a clear intent and the observer methods are self-documenting. Here is the implementation:
#import <AIObservable.h>
#import <NSInvocation+AIConstructors.h>
@interface AIChatRoom ()
// We use composition but you could also use inheritance
@property (nonatomic, strong) AIObservable* observable;
@end
@implementation AIChatRoom
- (void)postMessage:(AIMessage*)message {
// ...
// Create an invocation that will be dispatched to every observer
AIChatRoom* chatRoom = self;
NSInvocation* invocation = [NSInvocation invocationWithProtocol:@protocol(AIChatRoomObserver)
selector:@selector(chatRoom:didGetNewMessage:)];
[invocation setArgument:&chatRoom atIndex:2];
[invocation setArgument:&message atIndex:3];
[self.observable notifyObservers:invocation];
}
- (void)addObserver:(id<AIChatRoomObserver>)observer {
[self.observable addObserver:observer];
}
- (void)removeObserver:(id<AIChatRoomObserver>)observer {
[self.observable removeObserver:observer];
}
@end
Copy the source files to your project or add pod 'AIObservable'
to your Podfile.