Created
January 10, 2018 03:33
-
-
Save RbBtSn0w/db0f77f097e57573aaab64964522cc61 to your computer and use it in GitHub Desktop.
Obj-C delay send message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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