1

我有一个使用以下代码创建的 SocketCommunication 类

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);
self.inputStream = (__bridge NSInputStream *)readStream;
self.outputStream = (__bridge NSOutputStream *)writeStream;

_inputStream.delegate = self;
_outputStream.delegate = self;

[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[_inputStream open];
[_outputStream open];

一切正常。但是委托是在 UI 线程中调用的。在某些情况下,解析数据可能需要一些时间。

我如何告诉 NSInputStream 在后台线程中调用委托?

也许有一个自己的 NSRunLoop?

4

0 回答 0