title: Why does my array get cleared out by GCD dispatch to main queue?
body: <p>I have an app that has a tableviewcontroller with this viewDidLoad:</p>
  
  <pre><code>- (void)viewDidLoad{
      [super viewDidLoad];
      // begin animating the spinner
      [self.spinner startAnimating];
  
      [SantiappsHelper fetchUsersWithCompletionHandler:^(NSArray *users) {
          self.usersArray = [NSMutableArray array];
          for (NSDictionary *userDict in users) {
              [self.usersArray addObject:[userDict objectForKey:@"username"]];
          }
          //Reload tableview
          [self.tableView reloadData];
      }];
  }
  </code></pre>
  
  <p>The Helper Class method is this:</p>
  
  <pre><code>+(void)fetchUsersWithCompletionHandler:(Handler)handler {
  
      NSString *urlString = [NSString stringWithFormat:@"http://www.myserver.com/myApp/fetchusers.php"];
      NSURL *url = [NSURL URLWithString:urlString];
  
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
  
      [request setHTTPMethod: @"GET"];
  
      __block NSArray *usersArray = [[NSArray alloc] init];
  
      //A
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
          // Peform the request
          NSURLResponse *response;
          NSError *error = nil;
          NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
                                                       returningResponse:&amp;response
                                                                   error:&amp;error];
          if (error) {
              // Deal with your error
              if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                  NSLog(@"HTTP Error: %d %@", httpResponse.statusCode, error);
                  return;
              }
              NSLog(@"Error %@", error);
              return;
          }
  
          NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
  
          usersArray = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSASCIIStringEncoding] options:0 error:nil];
  
          if (handler){
              dispatch_async(dispatch_get_main_queue(), ^{
              handler(usersArray);
              });
          }
      });
  }
  </code></pre>
  
  <p>The above code was suggested to me and it makes sense from what I know about GCD.  Everything runs on the main queue, but before it dispatches to a background queue before the NSURLConnection synchronous call.  After it gets the data it fills the usersArray and should return it to the main queue.  The usersArray is populated and when it tests for if handler, it moves to the dispatch_asynch(dispatch_get_main_queue () line.  But when it returns to the main queue to process the array dictionaries, the NSArray *users is empty.  The app crashes with this error:</p>
  
  <p><strong>* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*</strong> -[__NSArrayM insertObject:atIndex:]: object cannot be nil' </p>
  
  <p>If I comment out the dispatch_async(dispatch_get_main_queue() code to look like this:</p>
  
  <pre><code>if (handler){
              //dispatch_async(dispatch_get_main_queue(), ^{
              handler(usersArray);
              //});
          }
  </code></pre>
  
  <p>It works fine...well kinda, its a little sluggish.  Why is this failing?</p>