title: How to integrate NSURLConnection with UIProgressView?
body: <p>I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the user while a file download is happening in the background.</p>
  
  <p>I created a separate object to download the file in the background, and I'm having problems figuring out how to update the progress property in the UIProgressView object with the correct value. It's probably something very simple, but I cannot figure it out with Googling around.</p>
  
  <p>Here's the code that I have:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
      [self.resourceData setLength:0];
      self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
      NSLog(@"content-length: %d bytes", self.filesize);
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      [self.resourceData appendData:data];
  
      NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
      NSLog(@"resourceData length: %d", [resourceLength intValue]);
      NSLog(@"filesize: %d", self.filesize);
      NSLog(@"float filesize: %f", [self.filesize floatValue]);
      progressView.progress = [resourceLength floatValue] / [self.filesize floatValue];
      NSLog(@"progress: %f", [resourceLength floatValue] / [self.filesize floatValue]);
  }
  </code></pre>
  
  <p>As you can see, the <b>resourceData</b> member variable holds the file data as it is being downloaded. The <b>filesize</b> member variable holds the full size of the file, as returned by my web service in its Content-Length header.</p>
  
  <p>It all works sort of OK, I keep getting the downloaded data with multiple executions of <b>didReceiveData</b> as I should, but when I try to calculate the progress value, no proper value is returned. See below for a small sample of what I get in my console log:</p>
  
  <pre><code>content-length: 4687472 bytes
  resourceData length: 2904616
  filesize: 4687472
  float filesize: -1.000000
  progress: -2904616.000000
  </code></pre>
  
  <p>For reference, <b>progressView.progress</b> is a float. <b>filesize</b> is a NSNumber that holds a long long. Finally, <b>resourceLength</b> is a NSNumber that holds a NSUInteger.</p>
  
  <p>What am I missing here?</p>