title: How to download a large file with the iPhone SDK and avoid memory usage issues?
body: <p>I'm using the <code>NSURLConnection</code> class to download a large file in my iPhone application, but it crashes every so often because it's using too much memory. I'm doing the usual <code>NSURLConnection</code> usage, to append the received data to a <code>NSMutableData</code> object.</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      [self.fileData appendData:data];
  }
  </code></pre>
  
  <p>Then after I finish downloading the whole file, I save it to a local temporary file, and read it as a mapped file like this:</p>
  
  <pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
      // save the downloaded data into a temporary file
      NSString *tempPath = NSTemporaryDirectory();
      NSString *tempFile = [tempPath stringByAppendingPathComponent:@"temp.pdf"];
      [self.fileData writeToFile:tempFile atomically:YES];
      NSData *mappedData = [NSData dataWithContentsOfMappedFile:tempFile];
  
      NSURL *baseURL = [NSURL URLWithString:@"http://mydomain.com"];
      [webView loadData:mappedData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:baseURL];
  }
  </code></pre>
  
  <p>What can I improve here to avoid these memory usage problems?</p>