Scheduling a thread on NSRunLoop

I followed this tutorial http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server and I have everything works fine, but there is one line in the text that I don't understand:

Our streams must always be ready to send or receive data. To enable this, the thread must be scheduled to receive events in the run loop. If we don't assign a run loop, the delegate will block our code execution until there is no stream data to read or write, which is a case we want to avoid.

But, if I comment out the lines in the code:

//[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

      

everything will work fine. I don't really understand why I should schedule this in the main run loop?

As I can see, everything that happens on the main thread is handled in this startup loop. So if I click the button, it gets processed in the main run loop. Here, if I don't schedule it in the main run loop, the bytes are still received and sent, so for me that means they are processed in the main run loop.

+3


source to share


1 answer


You can schedule your flow in any work area you want. The delegate callback methods will be called on the thread where it was scheduled. For example, I wrote some unit tests for my POSInputStreamLibrary where my delegate receives events on some kind of worker thread. You can see the planning process here



+1


source







All Articles