如何在从 NSMutableData 读取的 NSInputStream 上发出 NSStreamEventEndEncountered 信号
在 Cocoa 中,我设置了两个 NSThread,一个生产者和一个消费者。 生产者将数据附加到 NSMutableData,接收者从该数据打开 NSInputStream 并分块读取。
生产者线程的写入速度比消费者进程快很多,这是可以的。 但生产者只生产有限数量的工作,然后退出。 我希望消费者线程能够处理 NSStreamEventEndEncountered 事件。 当 NSInputStream 来自 NSMutableData 时,我如何发出信号?
我应该让生产者发送一系列带有表示流结束的神奇数字的字节吗?
In Cocoa, I've setup two NSThreads, one producer and one consumer.
The producer appends data to an NSMutableData, and the receiver opens an NSInputStream from that data and reads in chunks.
The producer thread writes a lot faster than the consumer processes, which is OK. But the producer only produces a finite amount of work, then exits. I'd like for the consumer thread to handle the NSStreamEventEndEncountered event. How can I signal this when the NSInputStream comes from an NSMutableData?
Should I just make the producer send a series of bytes with a magical number signifying an end of stream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是行不通的。 来自
-initWithData:
文档:这意味着,它将复制当时数据对象中的所有内容,而不是在向其中添加更多数据时遵循数据对象的内容。
(同样适用于
+inputStreamWithData:
。)尝试 管道 代替。
That won't work. From the
-initWithData:
documentation:Meaning, it will copy out whatever's in the data object at that time, not follow the contents of the data object as you add more data to it.
(The same goes for
+inputStreamWithData:
.)Try a pipe instead.