在objective-c中延迟方法调用

时间:2022-06-14 14:36:38

I know this can be done by using:

我知道这可以通过使用:

[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0]

However, the problem is that I want only 1 method call do be done. With this function the calls will stack on top of each other. I want to make a call and if another call is made the first one will be dismissed. Ideas?

但是,问题是我只想做一个方法调用。使用此功能,呼叫将堆叠在一起。我想拨打电话,如果再打电话,第一个电话将被解雇。想法?

4 个解决方案

#1


16  

Once the method is executing then there is no way of stopping it.

一旦该方法执行,则无法停止它。

But you can cancel if it is not fired. You can do something like this

但如果没有被解雇,你可以取消。你可以做这样的事情

//.... your code
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0];
//.... your code

In this way you can cancel previous perform request only if the myMethod is not being fired.

这样,只有在未触发myMethod的情况下,才能取消之前的执行请求。

#2


17  

In the Code Snippet Library in Xcode you can find one called GCD: Dispatch After, which looks like this:

在Xcode的代码片段库中,您可以找到一个名为GCD:Dispatch After的内容,如下所示:

double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        <#code to be executed on the main queue after delay#>
    });

Pretty self-explanatory.

非常不言自明。

#3


2  

EDIT: Now that I know that you want to only use the most recent, you could instead use:

编辑:既然我知道你只想使用最新的,你可以改为使用:

[self cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];

See this link for more info.

有关详细信息,请参阅此链接。

ORIGINAL POST:

原始邮寄:

You could just have a BOOL that is set to NO when it reaches that section and is then reset to YES after the method is performed.

您可以只有一个BOOL,当它到达该部分时设置为NO,然后在执行该方法后重置为YES。

So, for example, it would look something like:

因此,例如,它看起来像:

if (boolVal) {
    boolVal = NO;
    [self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0];
}

then in your myMethod, have:

然后在你的myMethod中,有:

boolVal = YES;

#4


2  

You should perform this selector in some other thread to avoid stack as you asked. use

你应该在其他一些线程中执行这个选择器,以避免你提出的堆栈。使用

[self performSelector:(SEL) onThread:(NSThread *) withObject:(id) waitUntilDone:(BOOL)];

In that selector you can add delay what ever you want. As this process will run in separate thread so will not stop others for the delay

在该选择器中,您可以根据需要添加延迟。由于此进程将在单独的线程中运行,因此不会阻止其他人延迟

#1


16  

Once the method is executing then there is no way of stopping it.

一旦该方法执行,则无法停止它。

But you can cancel if it is not fired. You can do something like this

但如果没有被解雇,你可以取消。你可以做这样的事情

//.... your code
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0];
//.... your code

In this way you can cancel previous perform request only if the myMethod is not being fired.

这样,只有在未触发myMethod的情况下,才能取消之前的执行请求。

#2


17  

In the Code Snippet Library in Xcode you can find one called GCD: Dispatch After, which looks like this:

在Xcode的代码片段库中,您可以找到一个名为GCD:Dispatch After的内容,如下所示:

double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        <#code to be executed on the main queue after delay#>
    });

Pretty self-explanatory.

非常不言自明。

#3


2  

EDIT: Now that I know that you want to only use the most recent, you could instead use:

编辑:既然我知道你只想使用最新的,你可以改为使用:

[self cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];

See this link for more info.

有关详细信息,请参阅此链接。

ORIGINAL POST:

原始邮寄:

You could just have a BOOL that is set to NO when it reaches that section and is then reset to YES after the method is performed.

您可以只有一个BOOL,当它到达该部分时设置为NO,然后在执行该方法后重置为YES。

So, for example, it would look something like:

因此,例如,它看起来像:

if (boolVal) {
    boolVal = NO;
    [self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0];
}

then in your myMethod, have:

然后在你的myMethod中,有:

boolVal = YES;

#4


2  

You should perform this selector in some other thread to avoid stack as you asked. use

你应该在其他一些线程中执行这个选择器,以避免你提出的堆栈。使用

[self performSelector:(SEL) onThread:(NSThread *) withObject:(id) waitUntilDone:(BOOL)];

In that selector you can add delay what ever you want. As this process will run in separate thread so will not stop others for the delay

在该选择器中,您可以根据需要添加延迟。由于此进程将在单独的线程中运行,因此不会阻止其他人延迟