Skip to content

Instantly share code, notes, and snippets.

@RbBtSn0w
Created January 10, 2018 03:33
Show Gist options
  • Save RbBtSn0w/db0f77f097e57573aaab64964522cc61 to your computer and use it in GitHub Desktop.
Save RbBtSn0w/db0f77f097e57573aaab64964522cc61 to your computer and use it in GitHub Desktop.
Obj-C delay send message.
@interface RBSComboTask : NSObject
{
@private
dispatch_block_t _handlerBlock;
BOOL _waitingTask;
}
- (void)performDelay:(NSTimeInterval)time handlerBlock:(nonnull dispatch_block_t)handlerBlock;
- (void)stopCallCompletionIfNecessary:(BOOL)callCompletion;
@end
@implementation RBSComboTask
- (void)_cleanDeon {
_waitingTask = NO;
_handlerBlock = nil;
}
- (void)_callTargetHandler {
if (_handlerBlock)
_handlerBlock();
[self _cleanDeon];
}
- (void)performDelay:(NSTimeInterval)time handlerBlock:(nonnull dispatch_block_t)handlerBlock {
[self stopCallCompletionIfNecessary:NO];
_waitingTask = YES;
_handlerBlock = handlerBlock;
[self performSelector:@selector(_callTargetHandler) withObject:nil afterDelay:time];
}
- (void)stopCallCompletionIfNecessary:(BOOL)callCompletion {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_callTargetHandler) object:nil];
if (callCompletion)
[self _callTargetHandler];
else
[self _cleanDeon];
}
- (void)dealloc {
[self stopCallCompletionIfNecessary:NO];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment