title: App hangs ( __Semwait_signal) when using performSelectorInBackground
body: <p>I am trying to call <code>pushIncompleteDataToServer</code> from  in <code>performSelectorInBackground</code> . It hangs the app. Looks some deadlock issue. I have no idea how to fix as it's the first time working on Threads in ios. It hangs in  <code>__Semwait_signal</code>  in system files.</p>
  
  <p>How to fix that.
  <strong>class1</strong></p>
  
  <pre><code>MatchDayDataController *sharedDataController = [MatchDayDataController sharedDataController];
  [sharedDataController performSelectorInBackground:@selector( pushIncompleteDataToServer) withObject:nil];
  </code></pre>
  
  <p><strong>MatchDayDataController class</strong></p>
  
  <pre><code>-(void) pushIncompleteDataToServer
  {
      NSManagedObjectContext *context = [self managedObjectContext];
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
      NSError *error;
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"MatchDayChecklist" inManagedObjectContext:context];
      NSPredicate *predicate = [NSPredicate predicateWithFormat: @"submited == %@" , @2];
      [fetchRequest setEntity:entity];
      [fetchRequest setPredicate:predicate];
      NSArray *record = [context executeFetchRequest:fetchRequest error:&amp;error];
      if (record.count&gt;0)
      {
          for (int i =0; i&lt;record.count; i++)
          {
  
          NSDictionary *afl =  [self getFormatedDictionary:[record objectAtIndex:i]];
  
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:afl options:0 error:&amp;error]; // Use options:NSJSONWritingPrettyPrinted to help with debugging JSON string
          if (!jsonData)
          {
              NSLog(@"Error with JSON data creation: %@", error);
              return;
          }
          // put it the fetched record in matchdaychecklist instacnce so to update submit =2 to 1 when submission is sucessful.
          MatchDayChecklist *matchdata = [record objectAtIndex:i];
  
          NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
          NSLog(@"Created JSON string: %@", jsonString);
          NSString* jsonDataLength = [NSString stringWithFormat:@"%d", [jsonString length]];
  
          NSURL* url = [NSURL URLWithString:@"ome urls"];
          NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
          [request setHTTPMethod:@"POST"];
          [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-type"];
          [request setValue:jsonDataLength forHTTPHeaderField:@"Content-length"];
          [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
  
          [NSURLConnection sendAsynchronousRequest:request
                                             queue:[NSOperationQueue mainQueue]
                                 completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
           {
               if (error != nil)
               {
                   NSLog(@"Submit failed due to: %@", error.localizedDescription);
                   matchdata.submited = @1;
                   NSLog(@"update for : %@" ,matchdata);
                   if (![context save:&amp;error])
                   {
                       NSLog(@"Error saving data.");
                   }
                   return ;
               }
  
               NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&amp;error];
  
               //data returned not in the format expected - failure
               if (dic == nil) {
                   NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                   //failure();
  
                   NSLog(@"Authentication service returned unexpected data: %@", str);
  
                   return;
               }
  
               //check if an error has been returned
               NSString *err = [dic objectForKey:@"message"]; //dic[@"error"]
               if (err != nil) {
                  // failure();
                   NSLog(@"Submit failed due to: %@", dic[@"error"]);
                   matchdata.submited = @1;
                   NSLog(@"update for : %@" ,matchdata);
                   if (![context save:&amp;error])
                   {
                       NSLog(@"Error saving data.");
                   }
                   return;
               }
  
               NSLog(@"Submit Successful");
  
               // Submit is success. so turn submit =2 =&gt;submit =1 and save the context.
  //             matchdata.submited = @1;
  //             NSLog(@"update for : %@" ,matchdata);
  //             if (![context save:&amp;error])
  //             {
  //                 NSLog(@"Error saving data.");
  //             }
  
           }];
          }
  
      }
  
  }
  </code></pre>