title: Accepted Method for Retrying URL Request in Objective-C?
body: <p>I have a connection request working using the stringWithContentsOfURL:encoding:error: method from NSString. I want to be able to retry the connection if it fails the first time. I was going to rewrite the function to use the timeout functionality of NSURLRequest, but I think I'd really like to use multiple retries before failing out.</p>
  
  <p>Aside from doing something crude, like a counter loop, is there an accepted way in Objective-C to retry a URL reuest for data?</p>
  
  <p>EDIT:
  Ok, I implemented NSURLConnection to get the data I'm interested in, and have added some rudimentary handling for errors using NSError. Here is my initial stab at this:</p>
  
  <pre><code>- (id)initWithURL:(NSURL*)aURL {
      NSError     *error      = nil;
      NSURLResponse   *response   = nil;
  
      NSURLRequest *request = [[NSURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
  
      NSData *data = [NSURLConnection sendSynchronousRequest:request  returningResponse:&amp;response error:&amp;error];
  
      NSString *xmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  
      if (response == nil) {
          if (error) {
              NSLog(@"Error has occurred: %@", [error localizedDescription]);
          }
      }
  }
  </code></pre>
  
  <p><em>Note: I haven't implemented a strategy to fail gracefully yet, I'm just logging the error for now.</em></p>
  
  <p>Using this code (or any suggested improvements), what would be the best way to implement a retry strategy here?</p>