Giter Club home page Giter Club logo

rtblockcallchecker's Introduction

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, [email protected]

License

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

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.