I have been following this tutorial http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server and I got everything working ok, but there is one line in text that I don't understand:
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.
But, if I comment the lines in code:
//[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
everything will still work fine. I don't really understand why I have to schedule this on main run loop?
As I see it everything that happens on main thread is being handled on this run loop. So if I press button it will be handled on main run loop. Here if I don't schedule this on main run loop, bytes are still getting received and sent, so to me that means they are procesed on main run loop.
You can schedule your stream in any runloop you want. Delegate's callback methods will be called in a such thread where it was scheduled. For example I wrote some unit tests for my POSInputStreamLibrary where my delegate receives events in a some worker thread. You can see scheduling process here