In the section "Stream Programming Guide: Poll vs. Cycle" Starting a Cycle ", the last couple says:
It should be noted that neither polling nor loop planning approaches is an airtight blocking protection. If NSInputStream hasBytesAvailableor NSOutputStream hasSpaceAvailablereturns NO, it means that in both cases the stream definitely has no bytes or spaces available. However, if YES is returned from these methods, it may mean that there are bytes or a space or that the only way to find out is to try to read or write operation (which may lead to an instant block). NSStreamEventHasBytesAvailableand NSStreamEventHasSpaceAvailableflow events have identical semantics.
Thus, it does not appear to be hasBytesAvailable/ hasSpaceAvailable, nor thread events guarantee blocking. Is there a way to get guaranteed non-blocking behavior with threads? I could create a background thread to get guaranteed non-blocking behavior, but I want to avoid this.
In addition, I do not understand why NSStreamit cannot provide guaranteed non-blocking behavior, given that low-level APIs (select, kqueue, etc.) can do this. Can someone explain why this is so?
source
share