当我按下一个按钮时,我需要调用一些函数。我使用[self getamountofdatacenters];来调用这些函数。当我按下按钮时,模拟器被卡住,直到完成所有功能,这需要5-7秒,所以我尝试使用NSThread:
[NSThread detachNewThreadSelector:@selector(GETAMOUNTDATACENTERS)
toTarget:self
withObject:nil];我用不同的函数做了三次,但我想找出NSThread的进度,并在它实际完成时捕获它。我该怎么做呢?谢谢。
发布于 2011-05-17 15:23:37
忘记显式线程,使用GCD
dispatch_async(dispatch_get_global_queue(0, NULL), ^{
[self getNumberOfDataCenters];
dispatch_sync(dispatch_get_main_queue(), ^{
// The work is done, do whatever you need.
// This code is executed on the main thread,
// so that you can update the UI.
});
});发布于 2011-05-17 15:08:10
一种选择是在应用程序级别“捕获”线程的执行结束:在发布到备用线程的代码中,在NSNotification完成时触发它。在主线程中,您可以观察此通知并做出相应的反应。
您还可以使用-isExecuting、-isFinished和-isCancelled检查线程的状态。
https://stackoverflow.com/questions/6027319
复制相似问题