rickytan / RTBlockCallChecker

A helper utility to check if a block has been called

Home Page:https://xcoder.tips/block-call-checker/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RTBlockCallChecker

CI Status Version License Platform

Introduction

Sometimes, as a third-party library, we must make sure that the completion block we passed to a developer must be called, or it will cause an error and unexpected states. For example, in WebKit, we have a navigation delegate method:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

What if for some reason the developer forget to call decisionHandler? This is not allowed. In fact, if you do forget to call it, WebKit will raise a exception for you. How did WebKit do that? well, it implements a CompletionHandlerCallChecker in CPP, and use the lamda expresion. But this project provides a tricky way in Objective-C. for more information, please read the source code, quite simple.

Usage

RTBlockCallChecker is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RTBlockCallChecker'
void (^someBlockMustBeCalled)() = ^{
   ...
};
// change
[self passBlockToAMethod:someBlockMustBeCalled];
// to
[self passBlockToAMethod:RT_CHECK_BLOCK_CALLED(someBlockMustBeCalled)];
// simple!

- (void)passBlockToAMethod:(void(^)(void))block {
    // 1. call the block immediatedlly
    block();        // ok
    // 2. call the block with delay
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block();    // ok
    });
    // 3. pass the block to another instance, and it will call the block in the future
    someObj.completion = block; // ok
    // 4. forget to call the block, raise an exception!
}

Author

Ricky Tan, ricky.tan.xin@gmail.com

License

RTBlockCallChecker is available under the MIT license. See the LICENSE file for more info.

About

A helper utility to check if a block has been called

https://xcoder.tips/block-call-checker/

License:MIT License


Languages

Language:Objective-C 90.6%Language:Ruby 9.4%