当前位置: 首页 > news >正文

Objective-C 版本的 LiveEventBus 效果

想要 Objective-C 版本的 LiveEventBus 效果(跨页面/跨模块通信,支持粘性和非粘性事件)。在 iOS 里对应的就是 NSNotificationCenter,但是它 默认不支持粘性事件,所以如果你想要“粘性”,需要自己封装一层。


1. 系统自带方式(非粘性事件)

这是最常见的写法(类似 Android 普通的 EventBus / LiveEventBus 的非粘性模式):

注册监听

// 在需要监听的类里(比如 viewDidLoad)
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(onUserLogin:)name:@"UserLoginNotification"object:nil];- (void)onUserLogin:(NSNotification *)notification {NSString *user = notification.userInfo[@"user"];NSLog(@"用户登录成功: %@", user);
}

发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"UserLoginNotification"object:niluserInfo:@{@"user": @"Tom"}];

特点:

  • 简单,零依赖。

  • 不支持“粘性事件”,也就是说如果事件在订阅前就发了,新订阅者不会收到。


2. 封装一个带粘性事件的 EventBus

自己写一个单例,存储最后一次事件,当新订阅时立即触发一次:

@interface EventBus : NSObject
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSNotification *> *stickyEvents;
+ (instancetype)shared;
- (void)post:(NSString *)name userInfo:(NSDictionary *)userInfo sticky:(BOOL)sticky;
- (void)observe:(NSString *)name observer:(id)observer selector:(SEL)selector sticky:(BOOL)sticky;
@end@implementation EventBus+ (instancetype)shared {static EventBus *instance;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{instance = [[EventBus alloc] init];instance.stickyEvents = [NSMutableDictionary dictionary];});return instance;
}- (void)post:(NSString *)name userInfo:(NSDictionary *)userInfo sticky:(BOOL)sticky {NSNotification *notification = [NSNotification notificationWithName:name object:nil userInfo:userInfo];if (sticky) {self.stickyEvents[name] = notification;}[[NSNotificationCenter defaultCenter] postNotification:notification];
}- (void)observe:(NSString *)name observer:(id)observer selector:(SEL)selector sticky:(BOOL)sticky {[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:nil];if (sticky) {NSNotification *last = self.stickyEvents[name];if (last) {// 手动立即分发上一次事件#pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-performSelector-leaks"[observer performSelector:selector withObject:last];#pragma clang diagnostic pop}}
}@end

使用示例

发布事件
[[EventBus shared] post:@"UserLoginNotification"userInfo:@{@"user": @"Tom"}sticky:YES];
订阅事件(新订阅者会马上收到粘性事件)
[[EventBus shared] observe:@"UserLoginNotification"observer:selfselector:@selector(onUserLogin:)sticky:YES];- (void)onUserLogin:(NSNotification *)notification {NSLog(@"粘性事件 - 登录用户: %@", notification.userInfo[@"user"]);
}

总结:

  • 如果只要普通通知 → 用 NSNotificationCenter

  • 如果要 LiveEventBus 粘性效果 → 用上面封装的 EventBus 单例。

http://www.dtcms.com/a/341015.html

相关文章:

  • vue+openlayers示例:适配arcgis矢量瓦片服务以及样式(附源码下载)
  • 英伟达Blackwell架构下的中国特供版AI芯片:B30A与RTX 6000D,是技术妥协还是市场新策略?
  • 基于单片机太阳能充电器/太阳能转换电能
  • C端高并发项目都有哪些
  • Angular由一个bug说起之十八:伴随框架升级而升级ESLint遇到的问题与思考
  • C++围绕音视频相关的资料都有哪些?如何进行学习
  • 实现自己的AI视频监控系统-第一章-视频拉流与解码2
  • 【ansible】4.实施任务控制
  • 【沉浸式解决问题】peewee.ImproperlyConfigured: MySQL driver not installed!
  • 亚马逊运营破局:销量与ACOS的动态平衡之道
  • 网页作品惊艳亮相!这个浪浪山小妖怪网站太治愈了!
  • 8 月中 汇报下近半个月都在做些什么
  • VR交通安全学习机-VR交通普法体验馆方案
  • Vue3源码reactivity响应式篇之数组代理的方法
  • Android studio gradle 下载不下来
  • 23种设计模式——模板方法模式(Template Method Pattern)详解
  • 在 Ubuntu Linux LTS 上安装 SimpleScreenRecorder 以录制屏幕
  • 软考中级习题与解答——第一章_数据结构与算法基础(1)
  • 软考网工选择题节选-2
  • uniapp:h5链接拉起支付宝支付
  • uni-app跨端开发最后一公里:详解应用上架各大应用商店全流程
  • 从协同设计到绿色制造:工业云渲染的价值闭环
  • uniapp 手写签名组件开发全攻略
  • 三极管单电源供电中电阻关系的理解
  • Oracle:创建触发器,当目标表相关字段有数据变动时,存入临时表
  • 开发避坑指南(29):微信昵称特殊字符存储异常修复方案
  • 0基础安卓逆向原理与实践:第5章:APK结构分析与解包
  • pinctrl和gpio子系统实验
  • 读者写者问题
  • 接地电阻柜的核心作用