I am making an iPhone app that connects to a server (whose protocol I have no control over) and gets a stream of textual data, where each line ends with a CRLF, and the amount of lines, as well as their exact length, is undetermined. I'm calling GCDAsyncSocket
's readDataToData:withTimeout:tag:
recursively to get the data, line by line.
I was previously reading from an NSStream
synchronously, so I would know when a read was complete by calling hasBytesAvailable
. I recently switched to GCDAsyncSocket
, and now I'd also like to know when the stream has read all available data, so I can save/process data, refresh a table, remove an overlay, etc.
I tried using the a similar technique as before, calling the following in my socket:didReadData:withTag:
delegate method, before the next read was requested:
[asyncSocket performBlock:^{
if (CFReadStreamHasBytesAvailable([sock readStream]))
NSLog(@"CFReadStreamHasBytesAvailable: yes");
else
NSLog(@"CFReadStreamHasBytesAvailable: no");
}];
But the result was always YES
.
I also tried calling the method progressOfReadReturningTag:bytesDone:total:
after the read request was sent, and it seems to always return 1 when the reads are finished and nil otherwise, but I'm not sure if it's reliable to use the method in that way.
Perhaps someone can tell me if progressOfReadReturningTag:...
is actually reliable in this case, or, if not, point me in another direction to solve this.