An NSNotificationCenter object

本文详细介绍了NSNotificationCenter(通知中心)的使用方法,包括如何通过addObserver:selector:name:object:方法注册观察者接收通知,通知的发送机制及注意事项。

 

NSNotificationCenter 对象 (通知中心) 提供一种在task内广播信息的机制,它基本上是一个通知调度表。通过调用addObserver:selector:name:object:方法来注册一个对象来接收通知中心的通知,此方法的每个调用指定了通知的一个集合,因此,对象可以调用多次该函数以注册为不同通知集合的观察者。

当某个对象post一个通告,它会发送一个NSNotification对象到通告中心,通告中心于是会以发送特定通告消息,并传递通告为唯一参数的方式来通知符合注册标准规定的观察者。

通告中心维持一个通告调配表,该表为特定观察者指定一个通过集。

一个通告集是发送到通告中心的通告的子集,每个表入口包含三个项目:

通告观察者

通告名

通告发送者

 

Table 1 shows the four types of dispatch table entries and the notification sets they specify. (This table omits the always present notification observer.)

Table 1  Types of dispatch table entries

Notification name

Notification sender

Notification set specified

Specified

Specified

Notifications with a particular name from a specific sender.

Specified

Unspecified

Notifications with a particular name by any sender.

Unspecified

Specified

Notifications posted by a specific sender.

Unspecified

Unspecified

All notifications.

Table 2 shows an example dispatch table with four observers.

Table 2  Example notification dispatch table

Observer

Notification name

Notification sender

observerA

NSFileHandleReadCompletionNotification

nil

observerB

nil

addressTableView

observerC

NSWindowDidChangeScreenNotification

documentWindow

observerC

nil

addressTableView

observerD

nil

nil

When notifications are posted to the notification center, each of the observers in Table 2 are notified of the following notifications:

  • observerA: Notifications named NSFileHandleReadCompletionNotification.

  • observerB: Notifications sent by addressTableView.

  • observerC: Notifications named NSWindowDidChangeScreenNotification sent by documentWindow and notifications sent byaddressTableView.

  • observerD: All notifications.

观察者接收通告的顺序是没有定义的。有可能发送对象和观察者对象是同一个对象。

通告中心同步传递通告给观察者,换句话说,postNotification: 方法会一直等到所有观察者接收和处理完通告后才返回。要异步发送通告可以使用NSNotificationQueue。在多线程应用中,通告总是在post通告的线程中传递,但可能与观察者注册自己不是同一个线程。

 

注意:通告中心不会保持观察者的引用,因此,必须确保在观察者销毁前反注册它们(usingremoveObserver: or removeObserver:name:object:),否则会产生运行时错误。

每个task都有一个默认的通过中心,通常不需要自己创建。NSNotificationCenter对象只能在单个task内传递通告。如果你想post通告到其它task或者从其它通告接收到通告,可以使用NSDistributedNotificationCenter对象。

 

 

用户升级ios26系统, 使用过程中LSSafeProtector这个第三方库的NSNotificationCenter报错, 如何解决, 中文方式说明, 下面是报错的方法以及错误信息 -(void)safe_addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject { [observer setIsNotification:YES]; [observer safe_changeDidDeallocSignal]; [self safe_addObserver:observer selector:aSelector name:aName object:anObject]; } #0 0x00000002478cc0cc in __pthread_kill () #1 0x00000001fa931810 in pthread_kill () #2 0x00000001ab154f1c in abort () #3 0x000000019cc43f60 in swift::fatalErrorv () #4 0x000000019cc43f80 in swift::fatalError () #5 0x000000019ccb19e4 in -[_TtCs12_SwiftObject doesNotRecognizeSelector:] () #6 0x000000019fb1f4f8 in ___forwarding___ () #7 0x000000019fb273a0 in _CF_forwarding_prep_0 () #8 0x000000010ea4db04 in -[NSNotificationCenter(Safe) safe_addObserver:selector:name:object:] at /Users/clouder/xiaohui-ios/powerone/3rdParty/LSSafeProtector/Foundation/NSNotificationCenter+Safe.m:103 #9 0x00000001a55f5e70 in ___lldb_unnamed_symbol297340 () #10 0x00000001a544dc4c in ___lldb_unnamed_symbol294151 () #11 0x00000001a544db1c in ___lldb_unnamed_symbol294149 () #12 0x00000001a54512b4 in ___lldb_unnamed_symbol294184 () #13 0x00000001a53eb2e4 in ___lldb_unnamed_symbol293679 () #14 0x00000001a544d39c in -[_UIAppCACommitFuture _invoke] () #15 0x00000001a05485bc in CA::Transaction::run_commit_handlers () #16 0x00000001a05090ac in CA::Context::commit_transaction () #17 0x00000001a0533ab0 in CA::Transaction::commit () #18 0x00000001a05413c0 in CA::Transaction::flush_as_runloop_observer () #19 0x00000001a543f0f0 in _UIApplicationFlushCATransaction () #20 0x00000001a543f024 in __setupUpdateSequence_block_invoke_2 () #21 0x00000001a544cee8 in _UIUpdateSequenceRunNext () #22 0x00000001a544c378 in schedulerStepScheduledMainSectionContinue () #23 0x0000000289db35f8 in UC::DriverCore::continueProcessing () #24 0x000000019fb43230 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ () #25 0x000000019fb431a4 in __CFRunLoopDoSource0 () #26 0x000000019fb20c6c in __CFRunLoopDoSources0 () #27 0x000000019faf68b0 in __CFRunLoopRun () #28 0x000000019faf5c44 in _CFRunLoopRunSpecificWithOptions () #29 0x000000023eec6498 in GSEventRunModal () #30 0x00000001a5470ddc in -[UIApplication _run] () #31 0x00000001a5415b0c in UIApplicationMain ()
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值