首次使用NSMachPort,是为了解决通知(NSNotification)如何接受异步消息的问题,场景很简单,通知的处理线程,在与通知的消息的发送,哪个线程发送,就在哪个线程处理,当异步发送通知,而需要同步到其他线程(例如主线程)处理通知消息时,就可以考虑采用NSMachPort做线程通信。
#define kMsg1 100
#define kMsg2 101
- (void)viewDidLoad {
[super viewDidLoad];
//1. 创建主线程的port
// 子线程通过此端口发送消息给主线程
NSPort *myPort = [NSMachPort port];
//2. 设置port的代理回调对象
myPort.delegate = self;
//3. 把port加入runloop,接收port消息
[[NSRunLoop currentRunLoop] addPort:myPort forMode:NSDefaultRunLoopMode];
NSLog(@"---myport %@", myPort);
//4. 启动次线程,并传入主线程的port
MyWorkerClass *work = [[MyWorkerClass alloc] init];
[NSThread detachNewThreadSelector:@selector(launchThreadWithPort:)
toTarget:work
withObject:myPort];
}
- (void)handlePortMessage:(NSMessagePort*)message{
NSLog(@"接到子线程传递的消息!%@",message);
//1. 消息id
NSUInteger msgId = [[message valueForKeyPath:@"msgid"] integerValue];
//2. 当前主线程的port
NSPort *localPort = [message valueForKeyPath:@"localPort"];
//3. 接收到消息的port(来自其他线程)
NSPort *remotePort = [message valueForKeyPath:@"remotePort"];
if (msgId == kMsg1)
{
//向子线的port发送消息
[remotePort sendBeforeDate:[NSDate date]
msgid:kMsg2
components:nil
from:localPort
reserved:0];
} else if (msgId == kMsg2){
NSLog(@"操作2....\n");
}
}