带有HTTPBody输入流的NSURLRequest:流在打开之前发送事件 [英] NSURLRequest with HTTPBody input stream: Stream sends event before being opened

查看:80
本文介绍了带有HTTPBody输入流的NSURLRequest:流在打开之前发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSURLConnection(和NSURLRequest)向服务器发送大量数据.为此,我创建了一对绑定的NSStream(使用CFStreamCreateBoundPair(...)).然后,将输入流传递到NSURLRequest(-setHTTPBodyStream:)并在当前运行循环中安排输出流.当运行循环继续进行时,我得到了事件以发送数据,输入流将该数据发送到服务器.

I want to send a large amount of data to a server using NSURLConnection (and NSURLRequest). For this I create a bound pair of NSStreams (using CFStreamCreateBoundPair(...)). Then I pass the input stream to the NSURLRequest (-setHTTPBodyStream:) and schedule the output stream on the current run loop. When the run loop continues, I get the events to send data and the input stream sends this data to the server.

我的问题是,这仅在数据适合配对流之间的缓冲区时才起作用.如果数据更大,那么输入流会以某种方式发生事件(我假设字节可用"),但是NSURLConnection尚未打开输入流.这会导致打印错误消息,并且没有发送数据.

My problem is, that this only works when the data fits into the buffer between the paired streams. If the data is bigger, then somehow the input stream gets an event (I assume "bytes available") but the NSURLConnection has not yet opened the input stream. This results in an error message printed and the data is not being sent.

我尝试通过简单地返回-stream:handleEvent:方法来捕获此问题,方法是简单地返回输入流是否尚未打开,但随后我的输出流却遇到了流关闭事件(可能是因为我从未尽可能地发送过数据).

I tried to catch this in my -stream:handleEvent: method by simply returning if the input stream is not yet opened, but then my output stream gets a stream closed event (probably because I never sent data when I could).

所以我的问题是:如何正确使用NSURLConnection绑定流对?

So my question is: How to use a bound pair of streams with NSURLConnection correctly?

(如果这很重要:我正在iOS平台上进行开发)

(If this matters: I'm developing on the iOS platform)

感谢您的帮助!

干杯,马库斯

推荐答案

好吧,我通过延迟上传来解决此问题,以便在NSURLConnection有时间设置其输入流之后重新启动.

Ok, I kind of fixed this by starting the upload delayed, so that it starts after the NSURLConnection had time to setup its input stream.

这不是我所说的干净的解决方案,因为依赖-performSelector:withObject:afterDelay:似乎有点不客气.

It's not what I call a clean solution though, since relying on -performSelector:withObject:afterDelay: seems a bit hacky.

因此,如果其他人对此有解决方案,我仍然愿意征求任何建议.

So if anyone else has a solution to this, I'm still open for any suggestions.

这篇关于带有HTTPBody输入流的NSURLRequest:流在打开之前发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆