gpt4 book ai didi

ios - 延迟后执行方法并取消它

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:13 26 4
gpt4 key购买 nike

我想在 2.0 秒后执行方法,但如果在 5 秒之前进行另一个调用,我希望能够取消它,但我注意到 cancelPreviousPerformRequestsWithTarget 发送对象 nil 和 performSelector发送对象NSString

它会产生问题吗?

-(void)startRecord:(NSString*)name {
if (self.needToStartRecording) {
//Cancel last call
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRecordAfterDelay:) object:nil];
}
self.needToStartRecording = YES;

[self performSelector:@selector(startRecordAfterDelay:) withObject:name afterDelay:5.0];
}

-(void)startRecordAfterDelay:(NSString*)name {
self.needToStartRecording = NO;

//Do My Stuff
}

最佳答案

根据文档,如果您传递nil,则只有使用nil 执行的请求才会被取消。您应该将以前的值存储在某处。

The argument for requests previously registered with the performSelector:withObject:afterDelay: instance method. Argument equality is determined using isEqual:, so the value need not be the same object that was passed originally. Pass nil to match a request for nil that was originally passed as the argument.

另外,如果你只有延迟使用这个选择器,你可以调用

[NSObject cancelPreviousPerformRequestsWithTarget:self]

这将取消之前发送给自己的所有请求

关于ios - 延迟后执行方法并取消它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com