title: NSStream reading\writing outside the delegate method handleEvent:eventCode
body: <p>in an iPhone app, I have a socket connection through wifi, and I need to read from inputStream and write to outputStream. The problem is that stream management is event-driven, and I have to wait for event NSStreamEventHasBytesAvailable before reading. So I can't know when reading\writing outside the handleEvent:eventCode delegate method.</p>
  
  <p>I tried a while loop, but I realized that during the while loop the app doesn't receive delegate messages and never stops:</p>
  
  <p>Pseudo-code:</p>
  
  <pre><code>-(void) myFunction {
     canRead=NO;
     [self writeToStream:someData];
     while(!canRead) { };
     readData=[self readFromStream];
  }
  
  - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
  
      switch(eventCode) {
            case NSStreamEventHasBytesAvailable: {
          canRead=YES;
          break;
        }
         }
  }
  </code></pre>
  
  <p>I think I could read\write inside the delegate method, but I need to read\write many times outside that.</p>
  
  <p>Help!
  Thankyou </p>