当前位置: 首页 > 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.lryc.cn/news/626482.html

相关文章:

  • java和javascript在浮点数计算时的差异
  • Flink实现Exactly-Once语义的完整技术分解
  • mac 搭建docker-compose,部署docker应用
  • Android 入门到实战(三):ViewPager及ViewPager2多页面布局
  • linux内核 - 内存管理单元(MMU)与地址翻译(二)
  • 0820 SQlite与c语言的结合
  • Mac编译Android AOSP
  • 【密码学实战】X86、ARM、RISC-V 全量指令集与密码加速技术全景解析
  • deque的原理与实现(了解即可)
  • HTML5中秋网站源码
  • 基于RK3568储能EMU,储能协调控制器解决方案
  • 生产电路板的公司有哪些?国内生产电路板的公司
  • MySQL 8.x的性能优化文档整理
  • RK3576赋能无人机巡检:多路视频+AI识别引领智能化变革
  • 【38页PPT】关于5G智慧园区整体解决方案(附下载方式)
  • 无人机图传 便携式5G单兵图传 HDMI图传设备 多卡5G单兵图传设备详解
  • 元宇宙的网络基础设施:5G 与 6G 的关键作用
  • 计算机视觉(二)------OpenCV图像视频操作进阶:从原理到实战
  • WIFI国家码修改信道方法_高通平台
  • 开发避坑指南(29):微信昵称特殊字符存储异常修复方案
  • 多模型创意视频生成平台
  • 微美全息(NASDAQ:WIMI):以区块链+云计算混合架构,引领数据交易营销科技新潮流
  • Linux: network: arp: arp_accept
  • imx6ull-驱动开发篇29——Linux阻塞IO 实验
  • Java并发容器详解
  • ubuntu go 环境变量配置
  • uv,下一代Python包管理工具
  • ⭐CVPR2025 给3D高斯穿 “UV 衣” 框架[特殊字符]
  • grpc 1.45.2 在ubuntu中的编译
  • 【软考架构】软件工程:软件项目管理