前言
我們打算通過博客與開源的方式,把在開發 Glow 和 Nurture 過程中積累的經驗分享出來。GLPubSub 作為開源計劃的第一步,是一個代碼量不多、卻相當實用的 Category。
GitHub
Documentation
通知(Notification)是發布訂閱模式(觀察者模式)的一種實現,在 iOS App 開發裡隨處可見。GLPubSub 對 NSNotificationCenter 做了一層封裝,目標是簡化 iOS 開發中的發布訂閱流程。由於它是 NSObject 的 Category,因此可以在任意 NSObject 的子類上使用。
安裝
CocoaPods
如果你用 CocoaPods 管理第三方依賴,可以:
在 Podfile 中加入以下依賴:
pod "GLPubSub", "~> 1.0"
執行 pod install 完成安裝
源文件
如果你的項目沒有借助 CocoaPods 管理依賴,也可以直接導入源文件:
下載最新代碼並解壓
把 NSObject+GLPubSub.h 和 NSObject+GLPubSub.m 導入工程,記得勾選 "Copy items if needed"
使用
由於 GLPubSub 基於 NSNotificationCenter,並注冊在 [NSNotificationCenter defaultCenter] 上,因此它也支持大部分系統通知,例如 UIApplicationDidEnterBackgroundNotification、UIApplicationDidBecomeActiveNotification、UITextFieldTextDidChangeNotification 等等,只不過在轉發過程會丟棄系統通知的 userInfo 字段。
設置 PubSub 的隊列
GLPubSub 主要建立在 NSNotificationCenter 的 -addObserverForName:object:queue:usingBlock: 方法之上。你可以調用 NSObject 的 +setPubSubQueue: 方法來指定傳入該方法的 queue。
默認傳入的 queue 為 nil,這意味著所有事件都會在發布通知的線程裡執行。你也可以手動設為 [NSOperationQueue mainQueue],讓所有事件都在主線程觸發:
[NSObject setPubSubQueue:[NSOperationQueue mainQueue]];
通過 Selector 訂閱事件
大多數時候,我們用 self 作為訂閱者:
[self subscribe:@"YourEventName" selector:@selector(yourEventHandler)];
你也可以指定事件的發布者:
[self subscribe:@"YourEventName" obj:somePublisher selector:@selector(yourEventHandler)];
如果你希望某個方法只觸發一次,可以這樣寫:
[self subscribeOnce:@"YourEventName" selector:@selector(yourEventHandler)];
這樣事件觸發一次後就會自動取消訂閱。
你的方法可以接收一個 GLEvent 參數,裡面攜帶了被觸發事件的相關信息。
@interface GLEvent : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, retain) id obj; @property (nonatomic, retain) id data;
其中 name 是事件名,obj 是發布者,data 是附加信息。
通過 Block 訂閱事件
寫法與前面用 selector 的方式類似。
GLEventHandler 定義如下:
typedef void (^GLEventHandler)(GLEvent *event);
於是可以這樣用 block 訂閱一個事件:
__weak __typeof__(self) weakSelf = self; [self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) { __strong __typeof__(weakSelf) strongSelf = weakSelf; [strongSelf appDidEnterBackground]; }];這裡的 weak 化是為了避免循環引用。對應 selector 那組方法,block 同樣提供 4 種調用方式:
- (id)subscribe:(NSString *)eventName handler:(GLEventHandler)handler; - (id)subscribe:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler; - (id)subscribeOnce:(NSString *)eventName handler:(GLEventHandler)handler; - (id)subscribeOnce:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler;
取消訂閱
取消訂閱某個事件:
- (void)unsubscribe:(NSString *)eventName;
取消訂閱所有事件:
- (void)unsubscribeAll;
雖然實例被銷毀時,存在 associated object 裡的觀察者也會一併釋放,但仍建議手動取消訂閱,比如按需求在 -dealloc 或 -viewDidDisappear 裡處理:
- (void)dealloc { [self unsubscribeAll]; }發布事件
你可以很簡單地發布一個事件:
[self publish:@"YourEventName"];
也可以附帶數據,很多時候我們會傳入一個 NSDictionary 來承載更結構化的內容:
[self publish:@"YourEventName" data:@{@"key": value}]循環引用
由於所有生成的觀察者都會被 self 引用,一旦你的 block 裡又引用了 self,就會形成循環引用,導致實例無法釋放,因此必須 weakify self。強烈推薦使用 libextobjc 中的 EXTScope:
weakify/strongify: @weakify(self); [self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) { @strongify(self); [self appDidEnterBackground]; }];License
GLPubSub 基於 MIT 協議開源,詳情見 LICENSE 文件。