在目标C中的函数调用后等待特定的时间

时间:2018-09-10 19:09:02

标签: objective-c timer thread-sleep

我有一个函数调用,调用后需要等待5秒钟。我迫不及待地使用[NSThread sleepForTimeInterval:5]或简单的sleep(5),因为它运行在主线程上,最终将阻塞整个应用程序。

需要的功能

function call sleep for 5 secs continue processing other functions

关于如何在目标C上做到这一点的任何想法?

1 个答案:

答案 0 :(得分:4)

dispatch_after怎么样?

https://developer.apple.com/documentation/dispatch/1452876-dispatch_after

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // throw your call to other functions in here
});