4

How to call NSStream synchronously to get the results??

Presently I am getting a async call back in one of its delegate methods

 `(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent`- 
Shri
  • 2,129
  • 2
  • 22
  • 32

1 Answers1

7

An NSStream is an abstract class that neither reads or writes data to a stream. To actually access the data you'll need a concrete subclass such as NSInputStream or NSOutputStream (or your custom subclass of NSStream). To read the data in an NSInputStream call read:maxLength:. You'll probably want to poll the stream, asking it if any new data is available, with hasBytesAvailable. An NSOutputStream has analogous write:maxLength: and hasSpaceAvailable methods.

You are highly encouraged by the iOS documentation to avoid polling, and use run-loop scheduling instead by responding to the async call back delegate methods.

Mr. Berna
  • 10,525
  • 1
  • 39
  • 42
  • 1
    If you are running in a separate thread it is not necessary to poll. You can just read from the input stream and it will block until there is data available/stream is closed/error occurs. Responses emulate posix behaviour, where 0 means stream closed, -1 means an error occurred. A positive number means actual bytes read from stream. – JugsteR Feb 16 '12 at 12:15