前段时间在写Promise时,调研了iOS有哪些通讯的方法。delegate
,notification
,GCD
是常见的方法,除此以外还有一些方法,在此记录共享一下。html
官方这样解释:ios
NSPipe
objects provide an object-oriented interface for accessing pipes. AnNSPipe
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.git
表示一个能够单向通讯的对象,只能一端读一端写。github
NSPipe
很简单,就两个属性:objective-c
@property (readonly, retain) NSFileHandle *fileHandleForReading; @property (readonly, retain) NSFileHandle *fileHandleForWriting;
跟上文表述一致。具体看这个例子吧,idea很赞。iOS IO 重定向(NSLog to UITextView)segmentfault
NSPipe
还能够用在socket中。NSPipe
用做通讯时,只能传递流式的数据。NSPipe
经过文件是能够跨进程通讯的。架构
dispatch_semaphore
经常使用做生产消费者模型中,是GCD
中用来作并发控制的。虽然不常见,但的确是能够经过dispatch_semaphore_create
dispatch_semaphore_signal
dispatch_semaphore_wait
这几个方法来进行通讯。并发
资料不少,随便搜。
遗憾的是,参数传递是个问题,并且用做线程间的通讯也很牵强,会让代码难于理解。app
NSPort
是一个通讯的通道,经过NSPortMessage
来传送消息socket
例子
- (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能传递msgid
和components
。msgid
是一个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
is a POSIX-compliant Unix system call that maps files or devices into memory.
在越狱机上能够经过mmap共享内存。但非越狱有沙盒,文件共享只能经过App Group
。暂时没有试过,先欠着,之后写demo吧。
Creating XPC Services 讲得很详细了
须要注意的是上述文章提到了:
错误隔离 (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 连接