前段時間在寫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 連接