Skip to content

Instantly share code, notes, and snippets.

@jottenlips
Forked from berzniz/NSObject+Debounce.h
Last active June 19, 2018 20:13
Show Gist options
  • Save jottenlips/97e8a07be3e45afd2c0d2d5016a21639 to your computer and use it in GitHub Desktop.
Save jottenlips/97e8a07be3e45afd2c0d2d5016a21639 to your computer and use it in GitHub Desktop.
Debounce method for Objective C
@interface NSObject (Debounce)
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay target:(id)target;
@end
@implementation NSObject (Debounce)
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay target:(id)target {
__weak typeof(self) weakSelf = target;
[NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:action object:nil];
[weakSelf performSelector:action withObject:nil afterDelay:delay];
}
@end
@jottenlips
Copy link
Author

Implementation [NSObject debounce:@selector(action) delay:2 target:self];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment