ios Objective-C/Cocoa 中 Java 的 Thread.sleep() 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829449/
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
What's the equivalent of Java's Thread.sleep() in Objective-C/Cocoa?
提问by levi
In Java you can suspend the current thread's execution for an amount of time using Thread.sleep()
. Is there something like this in Objective-C?
在 Java 中,您可以使用 暂停当前线程的执行一段时间Thread.sleep()
。Objective-C 中有这样的东西吗?
回答by smorgan
Yes, there's +[NSThread sleepForTimeInterval:]
是的,有+[NSThread sleepForTimeInterval:]
(Just so you know for future questions, Objective-C is the language itself; the library of objects (one of them at least) is Cocoa.)
(为了以后的问题,你知道,Objective-C 是语言本身;对象库(至少是其中之一)是 Cocoa。)
回答by Rok Strni?a
Sleeping for one secondin Java:
在 Java 中休眠一秒钟:
Thread.sleep(1000);
Sleeping for one secondin Objective C:
在目标 C 中睡一秒钟:
[NSThread sleepForTimeInterval:1.0f];
回答by Kendall Helmstetter Gelner
Why are you sleeping? When you sleep, you are blocking the UI and also any background URL loading not in other threads (using the NSURL asynchronous methods still operates on the current thread).
你为什么要睡觉?当您睡觉时,您将阻止 UI 以及任何不在其他线程中的后台 URL 加载(使用 NSURL 异步方法仍然在当前线程上运行)。
Chances are what you really want is performSelector:withObject:AfterDelay. That's a method on NSObject you can use to call a method at some pre-determined interval later - it schedules a call that will be performed at a later time, but all of the other stuff the thread handles (like UI and data loads) will still continue.
很有可能你真正想要的是 performSelector:withObject:AfterDelay。这是 NSObject 上的一个方法,您可以稍后使用它在某个预先确定的时间间隔调用一个方法——它安排了一个将在稍后执行的调用,但线程处理的所有其他内容(如 UI 和数据加载)将还是继续。
回答by Bill Heyman
Of course, you could also use the standard Unix sleep() and usleep() calls, too. (If writing Cocoa, I'd stay with the [NSThread sleepForTimeInterval:], however.)
当然,您也可以使用标准的 Unix sleep() 和 usleep() 调用。(但是,如果编写 Cocoa,我会继续使用 [NSThread sleepForTimeInterval:]。)
回答by wu liang
If you use NSThread sleepForTimeInterval(commented code) to sleep, fetching data will be blocked, but +[NSThread sleepForTimeInterval:] (checkLoad method) will not block fetching data.
如果使用 NSThread sleepForTimeInterval(commented code) 休眠,会阻塞取数据,但 +[NSThread sleepForTimeInterval:] (checkLoad 方法) 不会阻塞取数据。
My example code as below:
我的示例代码如下:
- (void)viewDidAppear:(BOOL)animated
{
//....
//show loader view
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON data"];
// while (_loans == nil || _loans.count == 0)
// {
// [NSThread sleepForTimeInterval:1.0f];
// [self reloadLoansFormApi];
// NSLog(@"sleep ");
// }
[self performSelector:@selector(checkLoad) withObject:self afterDelay:1.0f];
}
-(void) checkLoad
{
[self reloadLoansFormApi];
if (_loans == nil || _loans.count == 0)
{
[self performSelector:@selector(checkLoad) withObject:self afterDelay:1.0f];
} else
{
NSLog(@"size %d", _loans.count);
[self.tableView reloadData];
//hide the loader view
[HUD hideUIBlockingIndicator];
}
}
回答by j2emanue
usleep() can also be used as ive used this to pause the current thread at times
usleep() 也可以用作我有时用它来暂停当前线程