线程在运行过程中,可能需要与其它线程进行通信。Cocoa为iOS线程间通信提供2种方式,performSelector和Port。
(1) performSelector方式
在应用程序主线程中做事情:
performSelectorOnMainThread:withObject:waitUntilDone: performSelectorOnMainThread:withObject:waitUntilDone:modes:
在指定线程中做事情:
performSelector:onThread:withObject:waitUntilDone: performSelector:onThread:withObject:waitUntilDone:modes:
在当前线程中做事情:
performSelector:withObject:afterDelay: performSelector:withObject:afterDelay:inModes:
取消发送给当前线程的某个消息:
cancelPreviousPerformRequestsWithTarget: cancelPreviousPerformRequestsWithTarget:selector:object:
如在我们在某个线程中下载数据,下载完成之后要通知主线程中更新界面等等,可以使用如下接口:
- (void)myThreadMainMethod
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// to do something in your thread job
...
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO]; [pool release];
}
(2) NSMachPort方式
NSPort有3个子类,NSSocketPort、NSMessagePort、NSMachPort,但在iOS下只有NSMachPort可用。使用的方式为接收线程中注册NSMachPort,在另外的线程中使用此port发送消息,则被注册线程会收到相应消息,然后最终在主线程里调用某个回调函数。
可以看到,使用NSMachPort的结果为调用了其它线程的1个函数,而这正是performSelector所做的事情。线程间通信应该都通过performSelector来搞定。
使用方法如下:
.......
self.machPort = [NSPort port]; // [NSPort port]默认返回一个NSMachPort,NSMachPort的初始化方法总是返回nil
self.machPort.delegate = self;
[[NSRunLoop currentRunLoop] addPort:self.mainPort forMode:NSDefaultRunLoopMode]; // 工作线程的NSRunLoop
.......
- (void)trigerMachPort
{
if (self.machPort)
{
[self.machPort sendBeforeDate:[NSDate date] msgid:12 components:nil from:self.mainPort reserved:123];
}
[self performSelector:@selector(doSomething) onThread:self.thread withObject:nil waitUntilDone:NO];
}
- (void)handleMachMessage:(void *)msg
{
NSLog(@"message:%d", *(int *)msg);
}
另外,还可以采用GCD方式:
dispatch_async(dispatch_get_main_queue(), ^{
resultsTextView.text = resultsSummary;
});
转载于:https://blog.51cto.com/brilliantfuture/1425611