title: iOS method crashes when it's called while it's already running (using NSOperationQueue)
body: <p>I've a <code>UIViewController</code> that contains a <code>UITableView</code> and when I call the method "requireObjects", this last populate the arrays to configure the table. I state that the <code>cellForRowAtIndexPath</code> method work properly and dequeue a number of cells based on <code>[_arrID count]</code>.</p>
  
  <p>Each populated array has a matching index with all other arrays. For example <code>arrID[5] = 453</code> corresponds to <code>arrNames[5] = "Jean"</code> and corresponds to <code>arrStyle[5] = "Angry"</code> and corresponds to <code>arrLocation[5] = "New York"</code> and corresponds to <code>arrProfileSnaps[5] = jean.png</code>. </p>
  
  <p>These 5 arrays for example make up the fifth cell in the tableView. The method <strong>"requireObjects"</strong> succeeds and the table is built correctly. The problem arises when I call this method while it is still running. I wish that the method stops and repeats when I click a button. Or desperate times, it's good also that the method ends its execution and repeat when I click the button. But unfortunately my app crashes. Here the code:</p>
  
  <pre><code>- (void)requireObjects{
  
      @try {
          //threadsQueue is a NSOperationQueue allocated and initialized in viewDidLoad method
          [threadsQueue cancelAllOperations];
          [threadsQueue waitUntilAllOperationsAreFinished];
  
  
  
                  [_arrID removeAllObjects];
                  [_arrNames removeAllObjects];
                  [_arrStyle removeAllObjects];
                  [_arrLocation removeAllObjects];
                  [_arrProfileSnaps removeAllObjects];
  
              _arrNames = [[NSMutableArray alloc] init];
              _arrStyle = [[NSMutableArray alloc] init];
              _arrLocation = [[NSMutableArray alloc] init];
              _arrProfileSnaps = [[NSMutableArray alloc] init];
  
          [threadsQueue addOperation:[NSBlockOperation blockOperationWithBlock: ^{
  
              [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  
                  CGRect activityFrame = CGRectMake(self.view.center.x, self.view.center.y, 0.0, 0.0);
  
                  _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityFrame];
  
                  [[self.view superview] addSubview:_activityIndicator];
  
                  _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
                  _activityIndicator.color = [UIColor whiteColor];
                  [_activityIndicator startAnimating];
                  [_tableView reloadData];
              }];
  
  
          _arrID = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]];
  
              for (int i = 0; i &lt; [_arrID count]; i++) {
                  [_arrStyle addObject:@“..”];
                  [_arrNames addObject:@".."];
                  [_arrLocation addObject:@“..”];
                  [_arrProfileSnaps addObject:[UIImage imageNamed:@"blank"]];
              }
  
              [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
  
                  [self.tableView reloadData];
                  [_activityIndicator stopAnimating];
              }];
          }]];
      }
  
      @catch (NSException *exception) {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"An exception has occurred" message:[NSString stringWithFormat:@"%@",exception] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
          [alert show];
      }
  }
  </code></pre>
  
  <p>The app crashes in one iteration of the for loop at the point where I do <code>[_arrNames insertObject:...</code>. The error:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSRangeException', reason:
    '<em>*</em> -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty
    array'</p>
  </blockquote>
  
  <p>The strange thing is that the method succeeds any time I call it, but crashes only when I call it while it's already running!</p>