Created
January 25, 2014 16:18
-
-
Save berzniz/8618806 to your computer and use it in GitHub Desktop.
Debounce method for Objective C
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 NSObject (Debounce) | |
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay; | |
@end |
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
@implementation NSObject (Debounce) | |
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay | |
{ | |
__weak typeof(self) weakSelf = self; | |
[NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:action object:nil]; | |
[weakSelf performSelector:action withObject:nil afterDelay:delay]; | |
} | |
@end |
Awesome. Much shorter and better than my colleague's implementation. I've introduced this to him.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's my fork that adds a podspec file:
https://gist.github.com/maxgalbu/bec9cdb035051b0c4197
To use it as a pod, use this line in your Podfile:
pod 'NSObject+Debounce', :git => 'https://gist.github.com/maxgalbu/bec9cdb035051b0c4197.git'