起
前段時間在寫Promise時,調研了iOS有哪些通訊的方法。delegate
,notification
,GCD
是常見的方法,除此之外還有一些方法,在此記錄共享一下。
NSPipe
官方這樣解釋:
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.
表示一個可以單向通訊的物件,只能一端讀一端寫。
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能傳遞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
不算是一種通訊方式。
mmap
is a POSIX-compliant Unix system call that maps files or devices into memory.
在越獄機上可以通過mmap共享記憶體。但非越獄有沙盒,檔案共享只能通過App Group
。暫時沒有試過,先欠著,以後寫demo吧。
XPC Service
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 連結