如何让线程在 iOS 中休眠几秒钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28896008/
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
How to make thread sleep for seconds in iOS?
提问by Opart Code
In iOS env, is it possible to make current thread sleep for seconds, then execute my code? NSTimer, GDC or any technique is okay for me.
在 iOS 环境中,是否可以让当前线程休眠几秒钟,然后执行我的代码?NSTimer、GDC 或任何技术对我来说都可以。
回答by Popeye
It would be better if you shared what you have done but it here you go.
如果你分享你所做的事情会更好,但它在这里你去。
There are a few options you can go with:
您可以使用以下几个选项:
Option 1
选项1
// Standard Unix calls
sleep();
usleep();
Some documentation regarding the sleep
function can be found here. You'll find that they are actually C
functions but since Objective-C
is a strict superset of C we can still use the sleep
and usleep
functions.
sleep
可以在此处找到有关该功能的一些文档。您会发现它们实际上是C
函数,但由于Objective-C
是 C 的严格超集,我们仍然可以使用sleep
andusleep
函数。
Option 2
选项 2
[NSThread sleepForTimeInterval:2.000];//2 seconds
The Apple documentationfor this method states:
此方法的Apple 文档指出:
Sleeps the thread for a given time interval.
Discussion
No run loop processing occurs while the thread is blocked.
在给定的时间间隔内使线程休眠。
讨论
当线程被阻塞时,不会发生运行循环处理。
Option 3
选项 3
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
1 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
// Do whatever you want here.
});
The Grand Central Dispatch
route is a pretty good way of doing things as well. Here is the Apple Documentation for Grand Central Dispatchwhich is quite a good read.
该Grand Central Dispatch
航线是做事情,以及一个不错的办法。这是Grand Central Dispatch的 Apple 文档,值得一读。
There is also this question that might be pretty useful How to Wait in Objective-C
还有这个问题可能非常有用How to Wait in Objective-C
回答by Earl Grey
it cannot be easier.
再简单不过了。
sleep(seconds);
回答by Massimo Polimeni
Use the class method + (void)sleepForTimeInterval:(NSTimeInterval)ti
使用类方法 + (void)sleepForTimeInterval:(NSTimeInterval)ti
The variable NSTimeInterval is of type double and represents the number of seconds to sleep
变量 NSTimeInterval 是 double 类型,表示休眠的秒数
// Block for .5 seconds
[NSThread sleepForTimeInterval:.5];
回答by LoVo
Either
任何一个
[self performSelector:@selector(YourFunctionName) withObject:(can be Self or Object from other Classes) afterDelay:(Time Of Delay)];`
or
或者
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
//your method
});
回答by samir
it depends how you are creating (spawning) your threads. For example if you are creating your thread with NSThread
class, you can use the two class methods :
这取决于您如何创建(产生)您的线程。例如,如果您使用NSThread
类创建线程,则可以使用两个类方法:
sleepUntilDate:
sleepForTimeInterval:
But generally it's a bad idea to handle the threading management yourself, because multithreading programming is very hard. You can use GCD or operations queues for example to handle the multithreading in your application.
但通常自己处理线程管理是一个坏主意,因为多线程编程非常困难。例如,您可以使用 GCD 或操作队列来处理应用程序中的多线程。