本文介绍了如何检查是否有performSelector:等待被执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程式中,我有几个地方,我可以做一个

In my iPhone app I got several places where I can do a

[object performSelector:withObject:afterDelay:]

调用。所有导致相同的方法。现在,在该方法中,我想只执行一些代码的最新调用它。如果是第一个,我可以只做 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(thisMethod)object:nil] ,并确保它被调用一次。

call. All lead to the same method. Now, in that method I want to execute some code only on the latest call of it. If it was the first, I could just do [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(thisMethod) object:nil] and be sure it gets called once.

但是,有没有办法告诉是否有其他方法调用等待执行?

But, is there a way to tell if there are any other method calls waiting to be executed?

我可以在这个类中使用一个计数器,每次我设置这个延迟后执行,然后在每个调用的开始减少,只执行代码if此计数器置零。但我想知道是否是最好的/可接受的方法...

I could use a counter in this class that I would increment each time I set this perform after delay, then decrement it at start of each call and only execute the code if this counter is zeroed. But I'm wondering if it's the best/acceptable approach...

推荐答案

你可以做什么是这样的:调用 [object performSelector:withObject:afterDelay:] 只需添加上面提到的行。你实际上可以创建一个方法来处理你。例如:

Well what you could do is this: anytime you call [object performSelector: withObject: afterDelay:] just add the line you mentioned above it. You could actually create a method that handles that for you. For example:

- (void)performSelectorOnce:(SEL)selector withObject:(id)object afterDelay:(NSTimeInterval)delay
{
    [NSObject cancelPreviousPerformRequestsWithTarget:object selector:selector object:object];
    [self performSelector:selector withObject:object afterDelay:delay];
}

,并且使用 [object performSelector: withObject:afterDelay:] 只需用[self performSelectorOnce:withObject:afterDelay:]替换;

and than where ever you use [object performSelector: withObject: afterDelay:] just replace it with [self performSelectorOnce: withObject: afterDelay:];

如果要在多个对象

让我知道这是否适合你。

Let me know if this works for you.

这篇关于如何检查是否有performSelector:等待被执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:15