简体   繁体   English

如何使用NSFileHandle的writeabilityHandler?

[英]How to use NSFileHandle's writeabilityHandler?

Starting from OS X 10.7 and iOS 5.0 NSFileHandle has two new properties: readabilityHandler and writeabilityHandler. 从OS X 10.7和iOS 5.0开始,NSFileHandle有两个新属性:readabilityHandler和writeabilityHandler。 I tried to use writeabilityHandler, but no luck. 我试过使用writeabilityHandler,但没有运气。 The documentation is weird, it looks like they copy-pasted description of readabilityHandler and replaced word read with write . 文档很奇怪,它看起来像是readabilityHandler的复制粘贴描述,并用write替换了单词read

According to the documentation assigning the block should eventually call the block. 根据文档分配块最终应该调用块。 It does 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 handle synchronously [_incomingHandle writeData:dataToSend] which does not make sense. 只有当我尝试同步写入句柄[_incomingHandle writeData:dataToSend]它,这是没有意义的。 After it is called it throws an exception: EXC_BAD_INSTRUCTION 调用它之后会抛出异常:EXC_BAD_INSTRUCTION

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

I have also tried to send the data piece by piece. 我也尝试逐个发送数据。 No luck. 没运气。

Has anyone successfully used this property? 有没有人成功使用过这家酒店?

Hmm, do you have a sample project I could try? 嗯,你有一个我可以尝试的示例项目吗? I haven't had any issues with it, even when doing silly things like nesting handlers. 我没有遇到任何问题,即使在做嵌套处理程序等愚蠢的事情时也是如此。 A test case that shows it not working might help figure out what's wrong. 显示它不起作用的测试用例可能有助于找出问题所在。

One issue you could be running into is that the use of dispatch_io inside NSFileHandle will modify the properties of any fd you've passed in. That's arguably a bug in NSFileHandle, but is true for now :/ 您可能遇到的一个问题是在NSFileHandle中使用dispatch_io将修改您传入的任何fd的属性。这可以说是NSFileHandle中的一个错误,但现在是正确的:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM