All Projects → Lobster-King → SmartBlock

Lobster-King / SmartBlock

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

Programming Languages

objective c
16641 projects - #2 most used programming language

Projects that are alternatives of or similar to SmartBlock

Bblocationmanager
A Location Manager for easily implementing location services & geofencing in iOS. Ready for iOS 11.
Stars: ✭ 107 (+529.41%)
Mutual labels:  block, delegate
Windows.10.DNS.Block.List
Windows DNS Block List
Stars: ✭ 18 (+5.88%)
Mutual labels:  block
retro-winamp-block
A Winamp-styled audio block for all your retro music player needs.
Stars: ✭ 14 (-17.65%)
Mutual labels:  block
BlockSniper
An advanced (brush) world editing plugin for PocketMine-MP
Stars: ✭ 77 (+352.94%)
Mutual labels:  block
Simulink-Arduino-Serial
How to connect Arduino and Simulink
Stars: ✭ 54 (+217.65%)
Mutual labels:  block
hash
Data management, integration and modeling with blocks #
Stars: ✭ 400 (+2252.94%)
Mutual labels:  block
libclosure-KCBuild
libclosure-74-79编译 - 最新Block底层源码编译,大家可以轻松Block源码调试 查看_Block_copy 还有 __block底层操作...
Stars: ✭ 71 (+317.65%)
Mutual labels:  block
ban-peers
Checking & banning BitTorrent leech peers via Web API, remove ads, working for μTorrent. 通过网页 API 检查并屏蔽 BitTorrent 吸血对端, 移除广告, 工作于 μTorrent。
Stars: ✭ 21 (+23.53%)
Mutual labels:  block
BIPES
BIPES: Block based Integrated Platform for Embedded Systems allows text and block based programming for several types of embedded systems and Internet of Things modules using MicroPython, CircuitPython, Python or Snek. You can connect, program, debug and monitor several types of boards using network, USB or Bluetooth. No software install needed!
Stars: ✭ 72 (+323.53%)
Mutual labels:  block
block-site
Chrome extension that blocks access to distracting websites to improve your productivity.
Stars: ✭ 81 (+376.47%)
Mutual labels:  block
sleepover
💤 Sleep, snooze & step methods
Stars: ✭ 13 (-23.53%)
Mutual labels:  block
chainDB
A noSQL database based on blockchain technology
Stars: ✭ 13 (-23.53%)
Mutual labels:  block
conceal-explorer
Conceal Explorer - CCX Block Explorer
Stars: ✭ 26 (+52.94%)
Mutual labels:  block
Discord-Block-Bypass
Simple script that utilities discord's flaw in detecting who blocked who.
Stars: ✭ 24 (+41.18%)
Mutual labels:  block
Selector-Closure
A light way to convert objc target-action style to closure
Stars: ✭ 14 (-17.65%)
Mutual labels:  block
Crypto-Resources
Resources for trading Bitcoin and Altcoins
Stars: ✭ 22 (+29.41%)
Mutual labels:  block
slack-block-kit
DEPRECATED: Use https://github.com/slack-php/slack-php-block-kit instead
Stars: ✭ 30 (+76.47%)
Mutual labels:  block
GoHole
GoHole is a DNS server written in Golang with the same idea than the PiHole, blocking advertisements's and tracking's domains
Stars: ✭ 28 (+64.71%)
Mutual labels:  block
preact-delegate
Preact delegate DOM events
Stars: ✭ 17 (+0%)
Mutual labels:  delegate
lisk-builders
🛠️ A complete directory of Lisk delegates that also actively contribute
Stars: ✭ 17 (+0%)
Mutual labels:  delegate

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].