开发总结iOS知识栈

NSNotification的一些小知识

2018-04-12  本文已影响127人  雪山飞狐_91ae

notification即通知,当我们在不同类之间通信时就要用到通知方法。
使用notification,我们能够把消息发送给多个监听该消息的对象,而不需要知道监听该消息对象的任何信息。消息的发送者将消息发送给通知中心,接受消息者也只需要向通知中心注册自己感兴趣的消息即可。这样就降低了消息的发送者和接收者之间的耦合。

NSNotification

发送方将消息以NSNotification的形式发送给通知中心,然后通知中心将消息派发给注册了该消息的接收方。

@property (readonly, copy) NSString *name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;
- (instancetype)initWithName:(NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo

类方法:

+ (instancetype)notificationWithName:(NSNotificationName)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

发送通知

发送通知的方法主要有下列几种:

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

注册监听者

注册监听者有下列几个方法:

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;
- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;

很多人不明白这里的object指的是什么,在发送通知的时候也会传递一个object参数,一般情况下发送通知的object参数传递的是发送方自己,那么在注册监听者这里,object参数指代的也是发送方的这个object参数,意思就是接收object对象发出的名为name的通知,如果有其它发送方发出同样name的通知,是不会接收到通知的。如果把name和object这两个参数同时置为nil,则会接收所有的通知。这个可以自行测试。

移除监听者

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;

在iOS9以后已经不需要手动移除监听者。

NSNotificationQueue(通知队列)

//类方法返回当前线程的默认的NSNotificationQueue。
defaultQueue
- (void)enqueueNotification:(NSNotification *)notification postingStyle:(NSPostingStyle)postingStyle coalesceMask:(NSNotificationCoalescing)coalesceMask forModes:(nullable NSArray<NSRunLoopMode> *)modes;

上面这个方法是使用NSNotificationQueue来发送通知用的。这里面有四个参数。

typedef NS_ENUM(NSUInteger, NSPostingStyle) {
    NSPostWhenIdle = 1,
    NSPostASAP = 2,
    NSPostNow = 3
};

NSPostingStyle即指发送通知的方式,一共有三种方式。

  1. NSPostWhenIdle
    通过字面意思大概可以知道是在空闲时发送。
    简单地说就是当本线程的runloop空闲时即发送通知到通知中心。
  2. NSPostASAP
    ASAP即as soon as possible,就是说尽可能快。
    当当前通知或者timer的回调执行完毕时发送通知到通知中心。
  3. NSPostNow
    多个相同的通知合并之后马上发送。
typedef NS_OPTIONS(NSUInteger, NSNotificationCoalescing) {
    NSNotificationNoCoalescing = 0,
    NSNotificationCoalescingOnName = 1,
    NSNotificationCoalescingOnSender = 2
};
  1. NSNotificationNoCoalescing
    不管是否重复,不合并。
  2. NSNotificationCoalescingOnName
    按照通知的名字,如果名字重复,则移除重复的。
  3. NSNotificationCoalescingOnSender
    按照发送方,如果多个通知的发送方是一样的,则只保留一个。

同步与异步发送

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

同步指的是,当发送方发送通知后,必须要等到所有的监听者完成监听回调,发送方才会接着执行下面的代码。所以如果监听者的回调有大量的计算要处理的话,发送方会一直等待,只有回调全部结束才接着往下执行。

- (void)enqueueNotification:(NSNotification *)notification postingStyle:(NSPostingStyle)postingStyle;
- (void)enqueueNotification:(NSNotification *)notification postingStyle:(NSPostingStyle)postingStyle coalesceMask:(NSNotificationCoalescing)coalesceMask forModes:(nullable NSArray<NSRunLoopMode> *)modes;

异步发送通知即只要发送方的通知发送出去了,不管监听方的回调是否执行完毕,反正我就开始执行下面的代码。
但是!!!需要注意的是,当NSPostingStyle的类型是NSPostWhenIdle和NSPostASAP时确实是异步的,而当类型是NSPostNow时则是同步的。

上一篇下一篇

猜你喜欢

热点阅读