title: Error occurred with NSURLConnection via 3G
body: <p>I am using the IOS sdk to upload file, works fine over Wifi, but over 3G sometimes on large files the following error is caught in </p>
  
  <pre><code>- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)anError ..... 
  </code></pre>
  
  <p>"NSURLErrorDomain -1021 request body stream exhausted". </p>
  
  <p>I know i can override this problem by implementing the method: </p>
  
  <pre><code>- (NSInputStream*)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *) request 
  </code></pre>
  
  <p>so i did it. But when this method called i caught in didFailWithError next error "The operation couldn’t be completed. Cannot allocate memory".</p>
  
  <p>This error disappears if I add to method needNewBodyStream some delay. 
  Can somebody explain me for what need this delay, and how can i get rid of this hack? </p>
  
  <p>here is my code:</p>
  
  <pre><code>- (void) startUpload    
  {    
     NSInputStream* fileStream = [[NSInputStream alloc] initWithFileAtPath: sourcePath];
  
     [self.request setHTTPMethod:@"PUT"];
  
     [self.request setValue:[NSString stringWithFormat: @"%lu", fileSize] forHTTPHeaderField: @"Content-Length"];
  
     [self.request setHTTPBodyStream: fileStream];
  
     NSURLConnection* newConnection = [[NSURLConnection alloc] initWithRequest: self.request delegate: self startImmediately: YES];
  
     self.connection = newConnection;
  
     [newConnection release];
     [fileStream release];
  }
  
  #pragma mark NSURLConnectionDelegate
  
  - (NSInputStream *) connection: (NSURLConnection *) aConnection needNewBodyStream: (NSURLRequest *) request
  {
     [NSThread sleepForTimeInterval: 2];
  
     NSInputStream* fileStream = [NSInputStream inputStreamWithFileAtPath: sourcePath];
  
     if (fileStream == nil)
     {
         NSLog(@"NSURLConnection was asked to retransmit a new body stream for a request. Returning nil will cancel the connection.");
     }
  
     return fileStream;
  }
  </code></pre>