addObserverForName:object:queue:usingBlock:

本文介绍iOS中NSNotificationCenter的高级使用技巧,包括如何利用addObserverForName方法注册观察者,以便在接收到特定通知时执行指定block。此外,还提供了具体的代码示例,展示如何监听本地变化通知。
addObserverForName:object:queue:usingBlock:
Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.
(添加一条记录到接收对象的调度表,记录包含一个通知队列,队列中添加有block,可选项:通知名称,发送对象)
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block
Parameters
name

The name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue.

(通知名称用来注册观察者;也就是说,只有包含这个名称的通知才能被添加到操作队列中去)

If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.

(如果名称为nil,通知中心将不使用通知名称来决定是否添加block到操作队列中去)

obj

The object whose notifications you want to add the block to the operation queue.

(一个对象,你将添加它的block到操作队列中)

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.

(如果对象为nil,通知中心将不使用通知发送者来决定是否添加block到操作队列中去)

queue
The operation queue to which block should be added.
If you pass nil, the block is run synchronously on the posting thread.
block

The block to be executed when the notification is received.

(当接受到通知时会执行这个block)

The block is copied by the notification center and (the copy) held until the observer registration is removed.

 (通知中心将持有这个对象的副本,直到对象取消注册)

The block takes one argument:
notification
The notification.
Return Value
An opaque object to act as the observer.

Discussion
If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).
(如果一个通知出发了多个观察者的block,这些blocks也许会被同一时间并发执行(除了他们各自的队列和当前线程))
The following example shows how you can register to receive locale change notifications.

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
    queue:mainQueue usingBlock:^(NSNotification *note) {
 
        NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
    }];
To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];
Availability
Available in iOS 4.0 and later
用户升级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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值