RTBlockCallChecker
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 unexpedted 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
rickytan, [email protected]
License
RTBlockCallChecker is available under the MIT license. See the LICENSE file for more info.