【发布时间】:2013-01-09 09:34:18
【问题描述】:
在我的编码经验中,我第一次在目标 C 中处理线程。在我的应用程序中,我需要在两个线程中下载一些资产。下载完成后,我必须启动我的主线程,这将在线程中使用下载的资产。所以我写了一些这样的代码
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,但它只等到第一个线程完成。 所以如果我想等到所有两个线程都完成我该怎么办?任何人都可以用示例代码 sn-ps 提出建议。
【问题讨论】:
-
我认为从根本上讲,您的线程架构在这里被破坏:阻塞主线程会使其在阻塞时对事件无响应。相反,您应该使用的模式是将完成通知发布到主线程。
标签: ios objective-c multithreading ios4