摘要:起前段时间在写时,调研了有哪些通信的方法。用作通信时,只能传递流式的数据。通过文件是可以跨进程通信的。信号量常用作生产消费者模型中,是中用来做并发控制的。虽然不常见但的确是可以通过这几个方法来进行通信。
起
前段时间在写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是一个通信的通道,通过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 ServiceCreating 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 链接
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/16438.html
摘要:今天又拾一下牙慧,汇总一下资料。何时初始化未知。是个数组,为水平有限,没看出规律。辅助函数由编译器生成。除了,其他三种都会分别调用下面的函数或者会根据的值来决定调用相应类型的或者例如上述代码中以的形式提供给和进行注入。 动因 上次写代码时需要深入了解Block。发现Block is nothing but a struct。今天又拾一下牙慧,汇总一下资料。顺便记录几个源码中的发现 值得...
摘要:四种状态运行状态暂停状态停止状态销毁状态运行状态处于返回栈的栈顶位置,正在运行与用户发生着交互,系统不愿回收此种状态的。和都是为了解决重复创建问题,的作用域是栈顶,的作用域是整个返回栈。 Context Android 系统组件不同于普通类对象,能够直接创建实例,需要各自的上下文环境——Context。 Context 上下文环境确保 Android 系统组件(Activity、Se...
摘要:四种状态运行状态暂停状态停止状态销毁状态运行状态处于返回栈的栈顶位置,正在运行与用户发生着交互,系统不愿回收此种状态的。和都是为了解决重复创建问题,的作用域是栈顶,的作用域是整个返回栈。 Context Android 系统组件不同于普通类对象,能够直接创建实例,需要各自的上下文环境——Context。 Context 上下文环境确保 Android 系统组件(Activity、Se...
摘要:本文对多线程基础知识进行梳理,主要包括多线程的基本使用,对象及变量的并发访问,线程间通信,的使用,定时器,单例模式,以及线程状态与线程组。源码采用构建,多线程这部分源码位于模块中。通知可能等待该对象的对象锁的其他线程。 本文对多线程基础知识进行梳理,主要包括多线程的基本使用,对象及变量的并发访问,线程间通信,lock的使用,定时器,单例模式,以及线程状态与线程组。 写在前面 花了一周时...
阅读 890·2021-11-15 18:11
阅读 2124·2021-10-13 09:39
阅读 2922·2021-09-22 15:33
阅读 3356·2021-09-06 15:01
阅读 3307·2021-09-01 11:42
阅读 2501·2021-08-24 10:03
阅读 3484·2021-07-29 13:50
阅读 2826·2019-08-30 14:08