我一直在关注这个教程http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server一切正常,但文本中有一行我不明白:
Our streams have to continuously be ready to send or receive data. To enable this we have to schedule the stream to receive events in a run loop. If we do not assign a run loop the delegate will block the execution of our code until there is no data on the stream to read or write, which is a case we want to avoid.
但是,如果我在代码中注释这些行:
//[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
一切都会正常进行。我真的不明白为什么我必须在主运行循环上安排这个?
在我看来,主线程上发生的所有事情都在这个运行循环中处理。因此,如果我按下按钮,它将在主运行循环中处理。在这里,如果我不在主运行循环中安排它,字节仍然会被接收和发送,所以对我来说这意味着它们在主运行循环中被处理。
最佳答案
您可以在任何您想要的运行循环中安排您的流。委托(delegate)的回调方法将在安排它的此类线程中调用。例如,我为我的 POSInputStreamLibrary 编写了一些单元测试,其中我的委托(delegate)在某个工作线程中接收事件。可以看到调度进程here
关于ios - 在 NSRunLoop 上调度流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488906/