Skip to content

用Block实现的通知替代方案,并且已实现在不同线程进行发送消息和执行Block,支持多参数传送,解决回调地狱问题,适用于组件化数据传输等。

License

Lobster-King/SmartBlock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SmartBlock

使用场景:

  • 跨多个界面回调,例如:ViewA -> ViewB -> ViewC 点击ViewC 回调给ViewA或者ViewA所在的controller。(解决多级回调等繁琐问题)
  • 轻量级的通知模式替代方案。

Demo演示:

Demo gif

使用方式:

  • 引入该分类头文件。
  • 调用observeCallBackUsingKey方法,传入key和callBack block。
  • 在需要处理回调的地方调用callBackUsingKey方法,传入key。

跨界面传值

/*注册*/
__weak typeof(self)weakSelf = self;
    [self observeCallBackUsingKey:@"touchCallBack" callBack:^(NSString *msg) {
        NSLog(@"%s",__func__);
        weakSelf.view.backgroundColor = [UIColor orangeColor];
    } destructionOption:BlockDestructionDefault];
/*发送*/
[self callBackUsingKey:@"touchCallBack",@"msg",nil];

Block执行线程和注册线程一致

/*注册*/
[NSThread detachNewThreadSelector:@selector(addObserver) toTarget:self withObject:nil];

- (void)addObserver {
    NSLog(@"注册block线程:%@",[NSThread currentThread]);
    __weak typeof(self)weakSelf = self;
    [self observeCallBackUsingKey:@"touchCallBack" callBack:^(NSString *msg) {
        NSLog(@"block执行线程:%@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
           weakSelf.view.backgroundColor = [UIColor orangeColor];
        });
    } destructionOption:BlockDestructionDefault blockRunModeOption:BlockRunModeOnObserverThread];
}
/*发送*/
[self callBackUsingKey:@"touchCallBack",@"msg",nil];

注意事项:

  • 提供BlockDestructionDefault和BlockDestructionBlockInvoked两种模式。
  • block入参个数与调用时的参数个数确保一致,否则会丢弃block的执行。
  • 谨慎使用option BlockDestructionBlockInvoked

与原生通知对比:

  • 使用通知忘记dealloc移除观察者在iOS9之前因为__unsafed_unretained会出现野指针崩溃。
  • 发送通知和接收通知的处理是同步的。
  • 如果要实现发送通知和接收通知在不同线程,系统原生通知实现比较复杂。
  • 系统原生通知不支持传不定参数,不够灵活。

TODO List:

  • 多线程安全问题。
  • 性能优化。

Have a problem?

You can contact me in the following ways

About

用Block实现的通知替代方案,并且已实现在不同线程进行发送消息和执行Block,支持多参数传送,解决回调地狱问题,适用于组件化数据传输等。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published