title: When downloading a file NSURLConnection does not call didFinishLoading on end of file, when download was paused and resumed
body: <p>I have the following Problem which drives me nuts for several weeks now.</p>
  
  <p>I'have a small framework for downloading files. This framework has the ability to pause and resume a filedownload.
  So far so good.
  Problem is, everytime i pause a download then, after i resume it, the <code>NSURLConnection</code> responsible for the download will not call <code>connectionDidFinishLoading</code>, if the downloadedbytes equals the expected filesize, but keeps on calling <code>connectionDidReceiveData</code> and thus corrupts my download.
  I have no idea why this should be. When i don't pause/resume the download, everything works fine.
  Here is the code of the methods to pause and resume the download.</p>
  
  <pre><code>- (id)pause 
  {
      [self.connection cancel];
      self.connection = nil;
      return self;
  }
  
  - (id)resume
  {
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:600];
  
      NSFileManager *manager = [NSFileManager defaultManager];
      NSString *localSavePath = self.savePath;
      if (failBlocks.count &gt; 0) {
          [self cancel];
          [self start];
      }
      else {
          if( ![manager fileExistsAtPath:localSavePath] )
          {
              [manager createFileAtPath:localSavePath contents:[NSData data] attributes:nil];
          }    
          if (self.downloadData.length &gt; 0) {
  
              log_muma2(@"Should resume url %@",self.url);
              // Define the bytes we wish to download.
              NSString *range = [NSString stringWithFormat:@"bytes=%i-", downloadData.length];
              [request setValue:range forHTTPHeaderField:@"Range"];
          }
          if (!self.connection) {
              NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
              self.connection = conn;
          }        
      }
      return self;    
  }
  </code></pre>
  
  <p>I would be really glad if someone could help me out of this.</p>
  
  <p>I already tested, if the already downloaded data is of the correct size and stuff like that. everything seems to be ok. </p>
  
  <p>Many thanks in Advance.
  Maverick</p>
  
  <p>=========Edit=========</p>
  
  <p>Here is the code of my <code>didReceiveData</code></p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
      amountDownloaded += data.length;    
      NSInteger receivedLen = [data length];
      bytesReceived = (bytesReceived + receivedLen);
      if(expectedSize != NSURLResponseUnknownLength) {
          progress = ((bytesReceived/(float)expectedSize)*100)/100;
          [self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];        
          percentComplete = progress*100;        
      }
  
      if (self.savePath == nil || [self.savePath isEqualToString:@""]) {
          [self.downloadData appendData:data];        
      }
      else {
          [self.downloadData appendData:data];
          NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.savePath];
          [handle seekToEndOfFile];
          [handle writeData:data];
  //        
      }    
      if (expectedSize &lt; bytesReceived)
      {
          NSLog(@"download exceeded expected size %f with %lld", expectedSize, bytesReceived);
          [self pause];
          [self cancel];        
          self.connection = nil;
          self.downloadData = nil;
          bytesReceived = 0;
          expectedSize = 0;
          amountDownloaded = 0;
          progress = 0;
          percentComplete = 0;
          [self start];
      }
  }
  </code></pre>