title: NSOperationQueue run same task again on response iOS
body: <p>In my project I need to send data to server, for that I've used the following code to achieve the task:</p>
  
  <pre><code>- (void)sendJSONToServer:(NSString *) jsonString
  {
  // Create a new NSOperationQueue instance.
  operationQueue = [NSOperationQueue new];
  //
  
  // Create a new NSOperation object using the NSInvocationOperation subclass to run the operationQueueTask method
  NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                          selector:@selector(operationQueueTask:)
                                                                          object:jsonString];
  // Add the operation to the queue and let it to be executed.
  [operationQueue addOperation:operation];
  }//End of sendJSONToServer method
  
  -(void) operationQueueTask:(NSString *) jsonString
  {
  //NSOperationQueue *remoteResultQueue = [[NSOperationQueue alloc] init];
  dispatch_queue_t myQueue = dispatch_queue_create("SERVER_QUEUE",NULL);
  dispatch_async(myQueue, ^{
      // Performing long running process
      // Sending json data to server asynchronously
      NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[jsonString length]];
  
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL_eg_http://www.example.com"]];
  
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
  
      [NSURLConnection sendAsynchronousRequest:request queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
       {
           NSLog(@"Response is:%@",[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
       }];
  
      dispatch_async(dispatch_get_main_queue(), ^{
          // Update the UI
          NSLog(@"Thread Process Finished");
      });
  });
  }//End of operationQueueTask method
  </code></pre>
  
  <p>By the above code I'm able to send data and get response.</p>
  
  <p>But when there is no internet the data will not be sent to server. How to handle this situation based on the response we get.</p>
  
  <p>Let's say we get response <code>success</code> on fair condition ans <code>false</code> on worst condition.</p>
  
  <hr>
  
  <p><strong>UPDATED CODE FOR RETRIES</strong></p>
  
  <pre><code>-(id)init
  {
  self = [super init];
  if (self != nil)
  {
      //initialize stuffs here
      pendingOperationQueue = [[NSMutableArray alloc] init];
      operationQueue = [NSOperationQueue new];
  }
  return self;
  }//End of init method
  
  - (void)sendJSONToServer:(NSString *) jsonString
  {
      NSOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operationQueueTask:) object:[NSString stringWithString:[pendingOperationQueue objectAtIndex:0]]];
  [operation start];
  }//End of sendJSONToServer method
  
  -(void) operationQueueTask:(NSString *) jsonString
  {
  //NSOperationQueue *remoteResultQueue = [[NSOperationQueue alloc] init];
  dispatch_queue_t myQueue = dispatch_queue_create("SERVER_QUEUE",NULL);
  dispatch_async(myQueue, ^{
      // Performing long running process
      // Sending json data to server asynchronously
      NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[jsonString length]];
  
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL_http://www/example.com"]];
  
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
  
      [NSURLConnection sendAsynchronousRequest:request queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
       {
           NSLog(@"Response is:%@",[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
  
           if([[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] rangeOfString:@"true"].location == NSNotFound)
           {
               // Add the operation to the queue and let it to be executed.
               NSLog(@"Failed To Add To Server, Rerunning the task");
           }
           else
           {
               NSLog(@"Successfully Added To Server");
               NSLog(@"ADDED_DATA_TO_SERVER: %@", jsonString);
               if([pendingOperationQueue count] &gt; 0)
               {
                   [pendingOperationQueue removeObjectAtIndex:0];
  
                   if([pendingOperationQueue count] &gt; 0)
                   {
                       NSOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operationQueueTask:) object:[NSString stringWithString:[pendingOperationQueue objectAtIndex:0]]];
                       [operation start];
                   }
               }
           }
       }];
  });
  }//End of operationQueueTask method
  </code></pre>