iOS-NSNotificationCenter通知原理解析
一、基本概念
NSNotification和NSNotificationCenter是使用观察者模式来实现的用于跨层传递消息。
NSNotificationCenter采用单例模式。
二、基本实现
通知实现由三个类组成:NSNotificationCenter、NSNotification、NSObserverModel,对应关系和接口设计:
NSNotificationCenter是注册中心,发通知时,发送的是一个NSNotification对象。
NSNotification里面包含发通知需要传递的内容:
name(通知的字符串),
object(发送通知的对象),
userInfo(需要传递的参数),
NSObserverModel是注册通知时需要保持的参数,包括:
observer(观察者对象),
selector(执行的方法),
notificationName(通知名字),
object(携带参数),
operationQueue(队列),
block(回调),
三:原理图:

四:注意点:
1、每次调用addObserver时,都会在通知中心重新注册一次,即使是同一对象,监听同一个消息,而不是去覆盖原来的监听。这样,当通知中心转发某一消息时,如果同一对象多次注册了这个通知的观察者,则会收到多个通知。
2、observer 观察者(不能为nil,通知中心会弱引用,ARC是iOS9之前是unsafe_unretained,iOS9及以后是weak,MRC是assign,所以这也是MRC不移除会crash,ARC不移除不会crash的原因)
五:仿照写一个通知中心:WYNotificationCenter,WYNotification,WYObserverModel
#import <Foundation/Foundation.h> @class WYNotification;
NS_ASSUME_NONNULL_BEGIN typedef void(^OperationBlock)(WYNotification *); @interface WYObserverModel : NSObject
@property (nonatomic, weak) id observer; //观察者对象
@property (nonatomic, assign) SEL selector; //执行的方法
@property (nonatomic, copy) NSString *notificationName; //通知名字
@property (nonatomic, strong) id object; //携带参数
@property (nonatomic, strong) NSOperationQueue *operationQueue;//队列
@property (nonatomic, copy) OperationBlock block; //回调
@end
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface WYNotification : NSObject @property (readonly, copy) NSNotificationName 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; @end NS_ASSUME_NONNULL_END
#import "WYNotification.h"
@implementation WYNotification
- (instancetype)initWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo {
self = [super init];
if (self) {
_name = name;
_object = object;
_userInfo = userInfo;
}
return self;
}
@end
#import "WYNotificationCenter.h"
#import "WYObserverModel.h"
#import "WYNotification.h" @interface WYNotificationCenter()
@property(nonatomic,strong)NSMutableDictionary *obsetvers;
@end @implementation WYNotificationCenter + (WYNotificationCenter *)defaultCenter {
static WYNotificationCenter *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
instance.obsetvers = [NSMutableDictionary dictionary];
});
return instance;
} - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString*)aName object:(nullable id)anObject
{ // 创建数据模型
WYObserverModel *observerModel = [[WYObserverModel alloc] init];
observerModel.observer = observer;
observerModel.selector = aSelector;
observerModel.notificationName = aName;
observerModel.object = anObject; // 如果不存在,才创建
if (![self.obsetvers objectForKey:aName]) {
NSMutableArray *arrays = [NSMutableArray array]; [arrays addObject:observerModel]; // 添加进 json 中
[self.obsetvers setObject:arrays forKey:aName];
} else {
// 如果存在,取出来,继续添加进对应数组即可
NSMutableArray *arrays = (NSMutableArray *)[self.obsetvers objectForKey:aName]; [arrays addObject:observerModel];
} } - (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(WYNotification *note))block
{
WYObserverModel *observerModel = [[WYObserverModel alloc] init];
observerModel.block = block;
observerModel.notificationName = name;
observerModel.object = obj;
observerModel.operationQueue = queue; //如果不存在,那么即创建
if (![self.obsetvers objectForKey:name]) { NSMutableArray *arrays = [[NSMutableArray alloc]init];
[arrays addObject:observerModel];
//填充进入数组
[self.obsetvers setObject:arrays forKey:name]; }else{
//如果存在,取出来,继续添加即可
NSMutableArray *arrays = (NSMutableArray*)[self.obsetvers objectForKey:name];
[arrays addObject:observerModel];
}
return nil;
}
- (void)postNotificationName:(nonnull NSString *)name object:(nullable id)objec {
[self postNotificationName:name object:objec userInfo:nil];
}; - (void)postNotificationName:(nonnull NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo {
WYNotification *notification = [[WYNotification alloc] initWithName:name object:object userInfo:userInfo];
[self postNotification:notification];
}
- (void)postNotification:(WYNotification *)notification
{
//name 取出来对应观察者数组,执行任务
NSMutableArray *arrays = (NSMutableArray*)[self.obsetvers objectForKey:notification.name];
NSLog(@"%@",arrays);
[arrays enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { //取出数据模型
WYObserverModel *observerModel = obj;
id observer = observerModel.observer;
SEL secector = observerModel.selector; if (!observerModel.operationQueue) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[observer performSelector:secector withObject:notification];
#pragma clang diagnostic pop
}else{ //创建任务
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ //这里用block回调出去
observerModel.block(notification); }]; // 如果添加观察者 传入 队列,那么就任务放在队列中执行(子线程异步执行)
NSOperationQueue *operationQueue = observerModel.operationQueue;
[operationQueue addOperation:operation]; } }]; } #pragma mark - 移除通知 - (void)removeObserver:(nonnull id)observer {
[self removeObserver:observer name:nil object:nil];
} - (void)removeObserver:(nonnull id)observer name:(nullable NSString *)name object:(nullable id)object {
// 移除观察者 - 当有 name 参数时
if (name.length > 0 && [self.obsetvers objectForKey:name]) {
NSMutableArray *arrays = (NSMutableArray *)[self.obsetvers objectForKey:name];
[arrays removeObject:observer];
} else {
// 移除观察者 - 当没有 name 参数时
if (self.obsetvers.allKeys.count > 0 && self.obsetvers.allValues.count > 0) {
NSArray *allKeys = self.obsetvers.allKeys; for (int i = 0; i < allKeys.count; i++) {
NSMutableArray *keyOfAllObservers = [self.obsetvers objectForKey:allKeys[i]]; BOOL isStop = NO; // 如果找到后就不再遍历后面的数据了 for (int j = 0; j < keyOfAllObservers.count; j++) {
// 取出数据模型
WYObserverModel *observerModel = keyOfAllObservers[j]; if (observerModel.observer == observer) {
[keyOfAllObservers removeObject:observerModel];
isStop = YES;
break;
}
} if (isStop) { // 找到了,退出循环
break;
}
}
} else {
NSAssert(false, @"当前通知中心没有观察者");
}
}
}
@end
六、参考:
https://www.jianshu.com/p/2c9fef32a383
https://blog.csdn.net/m0_37182854/article/details/78782724
iOS-NSNotificationCenter通知原理解析的更多相关文章
- IOS NSNotificationCenter 通知的使用
1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@" ...
- iOS 10 UserNotification框架解析 – 本地通知
iOS 10以前的通知比较杂乱,把本地通知和远程通知分开了,诞生了许多功能类似的API,很容易让初学者犯迷糊.而iOS 10的通知把API做了统一,利用独立的UserNotifications.fra ...
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- APPcrawler基础原理解析及使用
一.背景 一年前,我们一直在用monkey进行Android 的稳定性测试 ,主要目的就是为了测试app 是否会产生Crash,是否会有ANR,页面错误等问题,在monkey测试过程中,实现了脱离Ca ...
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- 开源磁力搜索爬虫dhtspider原理解析
开源地址:https://github.com/callmelanmao/dhtspider. 开源的dht爬虫已经有很多了,有php版本的,python版本的和nodejs版本.经过一些测试,发现还 ...
- iOS 10 UserNotifications 框架解析
摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- iOS - Notification 通知
1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...
- IOS的XML文件解析,利用了NSData和NSFileHandle
如果需要了解关于文档对象模型和XML的介绍,参看 http://www.cnblogs.com/xinchrome/p/4890723.html 读取XML 上代码: NSFileHandle *fi ...
随机推荐
- php一些易犯的错误
1.mysql数据库字段是区分大小写的.字段在数组中要小写.(数据库字段UE_account) 错误的:
- spring的父子关系
1.父容器不能拿子容器的资源 2.子容器可以拿到父容器的资源
- Java练习 SDUT-1132_斐波那契数列
C/C++经典程序训练2---斐波那契数列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写计算斐波那契(Fibon ...
- Laravel的请求声明周期
声明周期概述# 开始# public/index.php 文件是所有对Laravel应用程序的请求的入口点.而所有的请求都是经由你的Web服务器(Apache/Nginx) 通过配置引导到这个文件.i ...
- Lambda plus: 云上大数据解决方案
本文会简述大数据分析场景需要解决的技术挑战,讨论目前主流大数据架构模式及其发展.最后我们将介绍如何结合云上存储.计算组件,实现更优的通用大数据架构模式,以及该模式可以涵盖的典型数据处理场景. 大数据处 ...
- behavior planning——inputs to transition functions
the answer is that we have to pass all of the data into transition function except for the previous ...
- oracle 基础表的选择
基础表(Driving Table)是指被最先访问的表(通常以全表扫描的方式被访问). 根据优化器的不同, SQL语句中基础表的选择是不一样的. 如果你使用的是CBO (COST BASED OPTI ...
- 防止SyntaxHighlighter.js的闪烁闪一下的方法
SyntaxHighlighter.js是一个代码高亮的JS插件,使用也很简单,但是由于是浏览器段执行JS代码来着色,会出现视觉上闪一下的效果.比如你的20行代码网页打开显示高度为100px,但是Sy ...
- artTemplate模版引擎的使用
artTemplate: template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不 ...
- iptablesIP规则的保存与恢复
iptables-save把规则保存到文件中,再由目录rc.d下的脚本(/etc/rc.d/init.d/iptables)自动装载 使用命令iptables-save来保存规则.一般用 iptabl ...