title: NSURLConnection data received after connectionDidFinishLoading
body: <p>I have a question about NSURLConnection:
  I want download an image with:</p>
  
  <pre><code>NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
  </code></pre>
  
  <p>the first delegate called (correctly) is</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  </code></pre>
  
  <p>after this is called one time:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  </code></pre>
  
  <p>and immediately:</p>
  
  <pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  </code></pre>
  
  <p>up to here everything is correct, but after connectionDidFinishLoading is fired once again the didReceiveData delegate for the same connection, I identify connection by:</p>
  
  <pre><code>NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];
  </code></pre>
  
  <p>Is this possible? </p>
  
  <p>update, more infos:</p>
  
  <p>My app fire many simultaneous connections and I store infos for any connection in a dictionary, when a delegate was called I retrive connection info using the key: NSString * connectionKey = [[[connection originalRequest] URL] absoluteString]; for 99% of connections everything is all right but for one (avery time the same!) connection I have this behavior</p>
  
  <hr>
  
  <p>here the complete implementation:</p>
  
  <pre><code>    - (void)downloadFileFromUrl:(NSURL *)url
                       inPath:(NSString *)completeFilePath
            dataReceivedBlock:(void (^)(long long byteDownloaded ,long long totalByte))dataReceivedBlock
                     endBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders))endBlock
                    failBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders, NSError * error))failBlock
  {
      //save the connection infos       
  NSMutableDictionary * requestData = [[NSMutableDictionary alloc] init];
  if(dataReceivedBlock)
  [requestData setObject:[dataReceivedBlock copy] forKey:@"dataReceivedBlock"];
  if(endBlock)
  [requestData setObject:[endBlock copy] forKey:@"endBlock"];
  if(failBlock)
  [requestData setObject:[failBlock copy] forKey:@"failBlock"];
  
          [requestData setObject:[NSNumber numberWithBool:YES] forKey:@"usingBlock"];
          [requestData setObject: completeFilePath forKey:@"downloadDestinationPath"];
  
      //delete the file if already on fs
      if([[NSFileManager defaultManager] fileExistsAtPath: completeFilePath])
          [[NSFileManager defaultManager] removeItemAtPath:completeFilePath error:nil];
  
      // Create the request.
      NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:TIME_OUT];
      NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
      if (!theConnection)
      {
          // Inform the user that the connection failed.
          failBlock(completeFilePath,nil,[NSError errorWithDomain:@"Connection fail" code:0 userInfo:nil]);
      }
  
      //add connection infos to the requests dictionary
      NSString * connectionKey = [[[theConnection originalRequest] URL] absoluteString];
      [self.requests setObject:requestData forKey:connectionKey];
  }
  </code></pre>
  
  <p>here a delegate example:</p>
  
  <pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  
  {
  
  NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];
  NSMutableDictionary * requestData = [self.requests objectForKey:connectionKey];
  NSFileHandle *file = [requestData objectForKey:@"fileHandle"];
  [file closeFile];
  
  //se la richiesta usa o no block
  BOOL usingBlock = [[requestData objectForKey:@"usingBlock"] boolValue];
  if(usingBlock)
  {
      __block void (^endBlock)(NSString * , NSDictionary *) = [requestData objectForKey:@"endBlock"];
      if(endBlock)
          endBlock([requestData objectForKey:@"downloadDestinationPath"],[requestData objectForKey:@"responseHeaders"]);
  }
  else
      [self.delegate downloadEnded:[requestData objectForKey:@"responseHeaders"]];
  
  
  //elimino dati richiesta
  [self.requests removeObjectForKey:[NSString stringWithFormat:@"%@",connectionKey]];
  
  //Inibisco standby
  [UIApplication sharedApplication].idleTimerDisabled = NO;
  }
  </code></pre>