NSURLConnection and bound pair of streams (CFStrea

2019-08-29 05:31发布

问题:

I'm using SimpleURLConnections example to implement multipart/form-data POST request using bound pair of streams. It works great in Simulator and on iPhone 3GS/iPhone 4.

When I try it on iPhone 3G (running 3.1.3) no data gets sent to the server. NSURLConnection just keeps hanging until it times out. After some testing I figured that the problem only occurs when I write to the producer stream more than once per connection. Whenever I need to write more than once (that is when I have to respond to more than one NSStreamEventHasSpaceAvailable event) things stop working.

This looks like the same issue: NSURLRequest with HTTPBody input stream: Stream sends event before being opened, and it does provide a workaround, but I haven't figured out what exactly I need to delay in order to fix the problem.

回答1:

I had the same issue and it seems that the data will only be sent if it fits in the buffer of the network stack (this would explain why it works on simulator where the buffer is presumably much bigger than on the device).

The workaround we had for this was to delay scheduling of the producer stream. Using performSelector:withObject:afterDelay: we called a method which schedules the output stream on the run loop after 1 sec, which leaves the URL connection enough time to open the stream.