美文网首页
iOS 取消延迟执行的代码

iOS 取消延迟执行的代码

作者: Smallwolf_JS | 来源:发表于2018-07-03 11:00 被阅读73次
/****************   Delayed perform  ******************/

@interface NSObject (NSDelayedPerforming)

- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes;
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;

@end

@interface NSRunLoop (NSOrderedPerform)

- (void)performSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg order:(NSUInteger)order modes:(NSArray<NSRunLoopMode> *)modes;
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(nullable id)arg;
- (void)cancelPerformSelectorsWithTarget:(id)target;

@end

上面是apple提供的延时执行的方法

延迟执行方法:

[self performSelector:@selector(didRuninCurrModel:) withObject:[NSNumber numberWithBool:YES] afterDelay:3.0f];
[self performSelector:@selector(didRuninCurrModelNoArgument) withObject:nil afterDelay:3.0f];

需要注意的是参数的一致性,如下面的取消无法实现:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModel:) object:[NSNumber numberWithBool:NO]];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModel:) object:nil];

下面方法可以一次取消所有的延迟执行:

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[self class] cancelPreviousPerformRequestsWithTarget:self];

相关文章

网友评论

      本文标题:iOS 取消延迟执行的代码

      本文链接:https://www.haomeiwen.com/subject/jsrduftx.html