iOS 中通知機(jī)制NSNotification

iOS 中通知機(jī)制詳解

NSNotification 通知的對象,一條通知就是一個NSNotification對象,包含如下屬性:

@property (readonly, copy) NSNotificationName name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;

NSNotificationCenter 管理通知的類,負(fù)責(zé)添加,發(fā)送,移除通知

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

關(guān)于name和object

  1. name為空,接收所有通知
  2. name不空,添加通知的object空,仍然接收所有name相同的通知
  3. name不空,添加通知object不空,接收name和object都匹配的通知
  4. 發(fā)送通知的時候,是否指定object對接收者不影響
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;

移除通知,iOS8之前需要手動移除通知,iOS8之后系統(tǒng)會在dealloc調(diào)用時,執(zhí)行removeObserver移除所有通知

NSNotificationQueue 通知隊列,根據(jù)發(fā)送和合并策略,發(fā)送通知

  • 添加通知到隊列
- (void)enqueueNotification:(NSNotification *)notification postingStyle:(NSPostingStyle)postingStyle coalesceMask:(NSNotificationCoalescing)coalesceMask forModes:(nullable NSArray<NSRunLoopMode> *)modes;
  • 從隊列移除通知
- (void)dequeueNotificationsMatching:(NSNotification *)notification coalesceMask:(NSUInteger)coalesceMask;

關(guān)于通知隊列的兩個策略:

  1. 何時發(fā)送
typedef NS_ENUM(NSUInteger, NSPostingStyle) {
    NSPostWhenIdle = 1,//空閑時
    NSPostASAP = 2,//盡可能快
    NSPostNow = 3//立刻
};
  1. 怎樣合并
typedef NS_OPTIONS(NSUInteger, NSNotificationCoalescing) {
    NSNotificationNoCoalescing = 0,//從不
    NSNotificationCoalescingOnName = 1,//name 相同的合并
    NSNotificationCoalescingOnSender = 2//sender相同的合并
};

asap和whenIdle需要開啟當(dāng)前runloop,并處在添加時的runLoopMode下

  1. 通知是同步還是異步;

    主線程中通知是同步,會等待處理通知的代碼執(zhí)行完才執(zhí)行后面的代碼

    子線程中發(fā)送通知,響應(yīng)通知的代碼會在它發(fā)出通知的線程中執(zhí)行,所以是異步的

  2. 如果想在子線程發(fā)送通知,主線程響應(yīng)通知就需要手動回調(diào)到主線程執(zhí)行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容