How to use NSFileHandle writeabilityHandler?

Starting with OS X 10.7 and iOS 5.0, NSFileHandle has two new properties: readabilityHandler and writeabilityHandler. I tried to use writeabilityHandler but no luck. The documentation looks weird, it looks like they copied the readabilityHandler description and replaced the word with a record .

According to the documentation that assigns the block, it should eventually call the block. Is not.

- (void)sendResponse:(NSData*)dataToSend { _incomingHandle.writeabilityHandler = ^(NSFileHandle* fileHandle) { [fileHandle writeData:dataToSend]; // exception is thrown here fileHandle.writeabilityHandler = nil; }; // Above block is not called without this line: //[_incomingHandle writeData:dataToSend]; } 

It is called only if I try to write to the descriptor synchronously [_incomingHandle writeData:dataToSend] , which makes no sense. After it is called, it throws an exception: EXC_BAD_INSTRUCTION

 *** Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: '*** -[NSConcreteFileHandle writeData:]: Resource temporarily unavailable' 

I also tried to send data in parts. Bad luck.

Has anyone successfully used this property?

+6
source share
1 answer

Hmm, do you have a sample project that I could try? I had no problem with this even when doing stupid things like nested handlers. A test that shows that it is not working can help you find out what is wrong.

One problem you might run into is that using dispatch_io inside the NSFileHandle changes the properties of any fd that you went through with. This is probably a bug in NSFileHandle, but for now this is true: /

0
source

Source: https://habr.com/ru/post/899937/


All Articles