iOS - 带套接字的NSInputStream:后台线程

时间:2013-07-26 08:46:15

标签: ios sockets nsinputstream

我有一个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?

0 个答案:

没有答案