Giter Site home page Giter Site logo

lobster-king / smartblock Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 2.0 874 KB

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

License: MIT License

Objective-C 100.00%
ios block nsnotificationcenter nsnotification delegate

smartblock's Introduction

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

smartblock's People

Contributors

lobster-king avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.