xcode 在允许运行循环继续的同时延迟的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12865614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:48:45  来源:igfitidea点击:

Proper way to delay while allowing the run loop to continue

iosxcodecocoatimerunloop

提问by Locksleyu

I have a need to delay for a certain amount of time and yet allow other things on the same runloop to keep running. I have been using the following code to do this:

我需要延迟一段时间,但允许同一个 runloop 上的其他东西继续运行。我一直在使用以下代码来做到这一点:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];

This seems to do exactly what I want, except that sometimes the function returns immediately without waiting the desired time (1 second).

这似乎正是我想要的,除了有时函数会立即返回而无需等待所需的时间(1 秒)。

Can anyone let me know what could cause this? And what is the proper way to wait while allowing the run loop to run?

任何人都可以让我知道是什么导致了这种情况?在允许运行循环运行的同时等待的正确方法是什么?

NOTE: I want to delay in a manner similar to sleep(), such that after the delay I am back in the same execution stream as before.

注意:我想以类似于 sleep() 的方式进行延迟,以便在延迟之后我回到与以前相同的执行流中。

回答by AliSoftware

You should use GCD and dispatch_afterfor that. It is much more recent and efficient (and thread-safe and all), and very easy to use.

dispatch_after为此,您应该使用 GCD 。它更新和高效(并且是线程安全的),并且非常易于使用。

There is even a code snippet embedded in Xcode, so that if you start typing dispatch_afterit will suggest the snippet and if you validate it will write the prepared 2-3 lines for you in your code :)

甚至在 Xcode 中嵌入了一个代码片段,因此如果您开始输入dispatch_after它会建议代码片段,如果您验证它会在您的代码中为您编写准备好的 2-3 行:)

Code Snippet suggestion by Xcode

Xcode 的代码片段建议

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

回答by Joshua Nozzi

Use an NSTimer to fire off a call to some method after a certain delay.

使用 NSTimer 在一定延迟后触发对某个方法的调用。

回答by marcos1490

Have you tried performSelector:withObject:afterDelay:?

你试过performSelector:withObject:afterDelay:吗?

From the Apple documentation

来自苹果文档

Invokes a method of the receiver on the current thread using the default mode after a delay.

延迟后使用默认模式在当前线程上调用接收器的方法。

回答by nishant

I had a similar issue and this is my solution. Hope it works for others as well.

我有一个类似的问题,这是我的解决方案。希望它也适用于其他人。

__block bool dispatched = false;
while ( put your loop condition here )
{
    if (dispatched)
    {
        // We want to relinquish control if we are already dispatched on this iteration.
        [ [ NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate:[ NSDate date ] ];
        continue;
    }

    // mark that a dispatch is being scheduled
    dispatched = true;

    int64_t delayInNanoSeconds = (int64_t) (0.1 * (float) NSEC_PER_SEC);
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInNanoSeconds);

    dispatch_after(delayTime, dispatch_get_main_queue(), ^() {
       // Do your loop stuff here
       // and now ready for the next dispatch
       dispatched = false;
    } );
}  // end of while