I made an application which uses NSStream to etablish a connection to a telnet server.
When the connection is made, I send a first command. Then I use sleep(1); to make my application wait. Then the second command is sent.
The problem is that the entire GUI is stuck during the sleep(). I know that it’s not the “perfect” way to make a “pause” and I’d like to learn how to this properly. I heard good things about NSTimer but I’d like to have a concrete and “easy” way of using it, to simply replace my poor use of sleep().
You should be able to register some kind of callback with whatever procedure you’re using to establish the connection. Just let your code wait for that callback without doing anything at all.
In this case, using
NSStream, you need to schedule the stream on the run loop:The run loop is the construct that processes events for your application. When you use
sleep(), it is stopped, and your GUI can’t do anything. By adding the stream as input to the run loop, you allow them both to continue to work.You also must set a delegate object (
[stream setDelegate:self];, e.g.) which will recieve notifications when the stream has something to report. That delegate must implementstream:handleEvent:, which will be called with a reference to the stream and a code indicating what happened.