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

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.xdnf.cn/news/18271.html

相关文章:

  • 直流无刷(BLDC)电机、单相直流无刷电机、三相直流无刷电机、单相直流无刷电机驱动芯片
  • 齐次线性方程组最小二乘解
  • 从零开始学AI——13
  • Docker复杂安装--最详细的MySQL主从复制与Redis集群安装、主从复制、主从扩容主从缩容实战版
  • java线程池相关知识
  • XR(AR/VR/MR)芯片方案,Soc VS “MCU+协处理器”?
  • 【动态规划、dp】P4933 大师
  • pnpm : 无法加载文件 C:\Program Files\nodejs\pnpm.ps1,因为在此系统上禁止运行脚本。
  • C++之多态(从0到1的突破)
  • Python如何将两个列表转化为一个字典
  • 基于STM32的APP遥控视频水泵小车设计
  • Codeforces MIN = GCD
  • Python爬虫实战:研究dark-fantasy,构建奇幻文学数据采集分析系统
  • BM25 vs TF-IDF:经典文本检索方法的对比
  • 【39】OpenCV C++实战篇——直线拟合、直线测距、平行线段测距;(边缘检测,剔除噪点,轮廓检测,渐进概率霍夫直线)
  • Django管理后台结合剪映实现课件视频生成应用
  • MySQL架构
  • MySQL实战45讲 24-25
  • hadoop技术栈(九)Hbase替代方案
  • Linux 进程间通信(IPC):信号、共享内存
  • Vue3 el-table实现 将子表字段动态显示在主表行尾
  • MySQL 三大日志:redo log、undo log、binlog 详解
  • 在职老D渗透日记day21:sqli-labs靶场通关(第27a关)get联合注入 过滤select和union “闭合
  • 趣谈设计模式之策略模式-比特咖啡给你一杯满满的情绪价值,让您在数字世界里”畅饮“
  • 基于VLM 的机器人操作视觉-语言-动作模型:综述 2
  • 选项式api和组合式api
  • 如何将Date类型的数据转换为LocalDateTime类型
  • Git的初步学习
  • 【力扣 Hot100】 刷题日记——双指针的经典应用
  • RabbitMQ:SpringAMQP Fanout Exchange(扇型交换机)