在Java中,你可以使用Thread.sleep()
暂停当前线程的执行一段时间。Objective-C中是否有类似的方法呢?
在Java中,你可以使用Thread.sleep()
暂停当前线程的执行一段时间。Objective-C中是否有类似的方法呢?
是的,有+[NSThread sleepForTimeInterval:]方法。
(仅供参考,Objective-C是语言本身;对象库(至少其中之一)是Cocoa。)
在Java中睡眠一秒钟:
Thread.sleep(1000);
在 Objective C 中睡眠 一秒钟:
[NSThread sleepForTimeInterval:1.0f];
- (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];
}
}
在编程中,usleep()函数也可以用来暂停当前线程。