CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

DPMessageBus 1.0.1

DPMessageBus 1.0.1

Maintained by DancewithPeng.



  • By
  • 张鹏

MessageBus

[TOC]

消息总线,用于组件化的基础组件

导入

Swift Package

.package(url: "https://github.com/DancewithPeng/MessageBus", .upToNextMinor(from: "1.0.1"))

Cocoapods

pod 'DPMessageBus', '~> 1.0.1'

使用

初始化

MBMessageBus *messageBus = [[MBMessageBus alloc] initWithName:@"MyMessageBus"];

可以给MBMessageBus添加观察者,观察者这里使用了弱引用,添加后不需要手动移除。观察者需要遵循MBMessageObserver协议,用于处理收到的信息

[messageBus addObserver:self];

通过总线发送消息

// 发送同步信息
[messageBus sendMessage:@"MyMessage" withInfo:@{@"MyInfoKey": ...}];    

// 发送异步信息
[messageBus sendAsyncMessage:@"MyAsyncMessage" withInfo:@{@"MyInfoKey": ...} callback:^(NSDictionary<NSString *,id> * _Nonnull info) {
  	NSLog(@"callbackInfo: %@", info);
}];
    
// 发送广播信息
[messageBus postMessage:@"MyPostMessage" withInfo:@{@"MyInfoKey": ...}];

LICENSE

此项目采用MIT开源协议,点击查看详情