NSStream Handle Event Giving Status 4

守給你的承諾、 提交于 2019-12-06 02:11:18

Actually you're not doing anything wrong.

This event (it is NSStreamEventHasSpaceAvailable) usually occours after writing to the stream telling you that stream is ready for writing again and after opening a writable stream. Please refer to NSStream Class Reference or, to be exact: Stream Event Constants.

If you're not familliar to << operator, it means shift bits to left for n places (each shift equals to multiplying by 2). Translation would be:

typedef enum {
   NSStreamEventNone = 0,
   NSStreamEventOpenCompleted = 1,
   NSStreamEventHasBytesAvailable = 2,
   NSStreamEventHasSpaceAvailable = 4,
   NSStreamEventErrorOccurred = 8,
   NSStreamEventEndEncountered = 16
};

In many applications you will se this event simply ignored (not handled) because it usually occours very soon after writing to the stream. If something goes wrong you get NSStreamEventErrorOccurred or NSStreamEventEndEncountered and these are the ones you need to handle. You could use NSStreamEventHasSpaceAvailable as a flag that it is o.k. to send some more data.

You should also know that both streams (inputStream and outputStream) are calling the same delegate method. That's why you get two NSStreamEventOpenCompleted events to begin with. But again in many cases this shouldnt be a problem. You can always check which stream is the originator of the event if needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!