Ios 使主线程等待,直到所有其他线程在IPhone sdk中完成
在我的编码经验中,我第一次在objective C中处理线程。在我的应用程序中,我需要在两个线程中下载一些资产。下载完成后,我必须启动我的主线程,它将利用线程中下载的资源。所以我写了一些这样的代码Ios 使主线程等待,直到所有其他线程在IPhone sdk中完成,ios,objective-c,multithreading,ios4,Ios,Objective C,Multithreading,Ios4,在我的编码经验中,我第一次在objective C中处理线程。在我的应用程序中,我需要在两个线程中下载一些资产。下载完成后,我必须启动我的主线程,它将利用线程中下载的资源。所以我写了一些这样的代码 NSThread *threadPlayer1 = [[NSThread alloc]initWithTarget:self selector:@selector(getPlayer1Assets) object:nil]; [threadPlayer1 start];
NSThread *threadPlayer1 = [[NSThread alloc]initWithTarget:self selector:@selector(getPlayer1Assets) object:nil];
[threadPlayer1 start];
NSThread *threadPlayer2 = [[NSThread alloc]initWithTarget:self selector:@selector(getPlayer2Assets) object:nil];
[threadPlayer2 start];
[self performSelectorOnMainThread:@selector(introducePlayer1) withObject:nil waitUntilDone:YES];
我将waituntilDone写为Yes,但它只会等到第一个线程完成。
所以,如果我想等到所有的两个线程都完成了,我应该怎么做?任何人都可以建议使用示例代码片段。我建议的是使用。来自Pulse Engenering博客。花一点时间在上面,直到你掌握了这个想法 至于你的代码。我猜你是这样做的:
[self performSelectorOnMainThread:@selector(introducePlayer1) withObject:nil waitUntilDone:YES];
在主线程上。阅读报纸上关于它的评论,特别是最后一句话:
等待指定当前线程是否阻塞的布尔值,直到
在主屏幕上的接收器上执行指定的选择器后
线指定“是”以阻止此线程;否则,请指定“否”
让此方法立即返回
如果当前线程也是主线程,并且您为
使用此参数,消息将立即传递和处理
我认为,从根本上讲,您的线程体系结构在这里被破坏了:阻塞主线程会使它在被阻塞时对事件没有响应。相反,您应该使用的模式是将完成通知发布到主线程的模式;这是个糟糕的答案。@Grabunks没有一个链接是死的,伙计我得到了“Safari找不到服务器”的链接。无论如何,链接可能会消亡,因此“链接到答案”的答案是脆弱和糟糕的形式。