0

Sorry to bother with yet another NSURLConnection question, adding to the over one thousand already here.

The scenario is as follows. In an iPhone app using dynamically loaded augmented reality features, the user is prompted to download new AR models as these are made available. The models can be several MB large, so the user should be given an indication of the total size of all models to be downloaded before deciding to do so.

To find out how large each file is I want to use an asynchronous NSURLConnection but then to stop the download once I have got the response ([NSURLResponse expectedContentLength]). I can do this in the delegate's connection:didReceiveResponse: method.

My question is, how can I wait until this condition arises? How can I setup the NSURLConnection, let it start asynchronously and then wait until the connection:didReceiveResponse: method is called? I have tried using a NSCondition, letting this wait after setting up the NSURLConnection and in the connection:didReceiveResponse: method signalling the condition. But all this did was to freeze the main thread. Any ideas?

Brayden
  • 1,795
  • 14
  • 20
Paul Buller
  • 150
  • 7

1 Answers1

1

Maybe you could send a HEAD request instead of GET. This may depend on your server set up, but that should get you just the headers, including Content-Length. You ought to be able to use a NSMutableURLRequest so you can change the request method, and then read expectedContentLength on the response as usual.

Brian
  • 15,599
  • 4
  • 46
  • 63
  • Thx Brian, I was wondering how to send a HEAD request. If I did this, then I could use a synchronous NSURLConnection - with an appropriate timeout - wait till it completes and then read out the expectedContentLength. Thanks for the tip. – Paul Buller Sep 27 '12 at 21:13
  • Just to say your suggestions tests fine after getting back to work. I set up a NSMutableURLRequest, change the method to HEAD, do a NSURLConnection sendSynchronousRequest and read the expectedContentLength from the response, all without any threads, GCD or callbacks. Very elegant. Thanks again. – Paul Buller Sep 28 '12 at 11:54