2

前段时间在写Promise时,调研了iOS有哪些通信的方法。delegate,notification,GCD是常见的方法,除此之外还有一些方法,在此记录共享一下。

NSPipe

官方这样解释:

NSPipe objects provide an object-oriented interface for accessing pipes. An NSPipe object represents both ends of a pipe and enables communication through the pipe. A pipe is a one-way communications channel between related processes; one process writes data, while the other process reads that data. The data that passes through the pipe is buffered; the size of the buffer is determined by the underlying operating system. NSPipe is an abstract class, the public interface of a class cluster.

表示一个可以单向通信的对象,只能一端读一端写。

NSPipe很简单,就两个属性:

@property (readonly, retain) NSFileHandle *fileHandleForReading;
@property (readonly, retain) NSFileHandle *fileHandleForWriting;

跟上文表述一致。具体看这个例子吧,idea很赞。iOS IO 重定向(NSLog to UITextView)

NSPipe还可以用在socket中。NSPipe用作通信时,只能传递流式的数据。NSPipe通过文件是可以跨进程通信的。

信号量

dispatch_semaphore常用作生产消费者模型中,是GCD中用来做并发控制的。虽然不常见,但的确是可以通过dispatch_semaphore_create dispatch_semaphore_signal dispatch_semaphore_wait这几个方法来进行通信。

资料很多,随便搜。
遗憾的是,参数传递是个问题,而且用作线程间的通信也很牵强,会让代码难于理解。

NSPort

NSPort是一个通信的通道,通过NSPortMessage来传送消息

  • 例子

- (void) foo {
  NSPort *port = [NSMachPort port];
  port.delegate = self;

  [[NSRunLoop currentRunLoop] addPort:port forMode:NSDefaultRunLoopMode];

  SomeOtherWorker *worker = [[SomeOtherWorker alloc] init];
  [NSThread detachNewThreadSelector:@selector(launchWithPort:)
                             toTarget:worker
                           withObject:port];
}

- (void)handlePortMessage:(NSMessagePort*)message{
    NSUInteger msgId = [[message valueForKeyPath:@"msgid"] integerValue]; //[message msgid]
    NSPort *localPort = [message valueForKeyPath:@"localPort"];//[message receivePort]
    NSPort *remotePort = [message valueForKeyPath:@"remotePort"]//[message sendPort];
    ......
}

Worker Class

@implementation SomeOtherWorker
{
    NSPort* _remotePort;
    NSPort* _myPort;
}

- (void)launchWithPort:(NSPort *)port {
    _remotePort = port;
    [[NSThread currentThread] setName:@"SomeOtherThread"];
    [[NSRunLoop currentRunLoop] run];

    _myPort = [NSMachPort port];
    _myPort.delegate = self;

    [[NSRunLoop currentRunLoop] addPort:_myPort forMode:NSDefaultRunLoopMode];
    [_remotePort sendBeforeDate:[NSDate date]
                         msgid:kMsg1
                    components:nil
                          from:_myPort
                      reserved:0];
}

#pragma mark - NSPortDelegate 如不接收父线程的消息,则不用实现
- (void)handlePortMessage:(NSPortMessage *)message
{
}
@end
  • 要注意的
    NSPort能传递msgidcomponentsmsgid是一个uint,而components是这样说的:

The data to send in the message. components should contain only NSData and NSPort objects, and the contents of the NSData objects should be in network byte order.

运行时发现如果传NSData的话,拿到是个OS_dispatch_data类型的实例。暂时不太懂。

CF的使用方法参考这里

其他参考

mmap 共享文件

严格来讲mmap不算是一种通信方式。

mmap is a POSIX-compliant Unix system call that maps files or devices into memory.

在越狱机上可以通过mmap共享内存。但非越狱有沙盒,文件共享只能通过App Group。暂时没有试过,先欠着,以后写demo吧。

参考

XPC Service

Creating XPC Services 讲得很详细了

XPC(和翻译)也讲得很不错。

需要注意的是上述文章提到了:

错误隔离 (Fault Isolation) 和 权限隔离 (Split Privileges)

这是App架构设计的重要准则之一。

XPC 是跨进程的。iOS上无法使用,除非越狱。

参考

http://blog.csdn.net/yxh265/a...
https://github.com/stevestrez...
http://aron.cedercrantz.com/2...
https://github.com/a1anyip/li...
https://github.com/nevyn/Mesh...
http://blog.csdn.net/jia12216...
https://segmentfault.com/a/11...
http://www.tanhao.me/pieces/6...

原作写于segmentfault 链接


canopus4u
618 声望50 粉丝