title: Background computation on iPad blocks user interface
body: <p>I'm trying to use background threds to do some computations on an iPad. 
  The thing is even thou the computationa are running. The UI is blocked while they run...
  What am I doing wrong.</p>
  
  <pre><code>[mc evaluateFormula:adapted runNo:10000];
  </code></pre>
  
  <p>This is called from an IBAction.</p>
  
  <p>This is the code that is called:</p>
  
  <pre><code>-(void)evaluateFormula:(NSDictionary *)frm runNo:(NSUInteger)runCount
  {
  
      self.runCount = runCount;
      self.frm = frm;
      [self performSelectorInBackground:@selector(backgroundEvalFrm) withObject:nil];
  
  //    for (int i = 0; i &lt; runCount; i++) {
  //        [self runFormula:frm];
  //    }
  //    
  
  }
  
  -(void)backgroundEvalFrm
  {
      percentVal = self.runCount / 100;
      NSOperationQueue *queue = [[NSOperationQueue alloc] init];
      [queue setMaxConcurrentOperationCount:20];
  
  
  
      for (int i = 0; i&lt;self.runCount; i++) {
          NSInvocationOperation *op =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(runFormula:) object:self.frm];
          [queue addOperation:op];
  
      }
  
  }
  </code></pre>
  
  <p>So why is the UI blocked? </p>
  
  <p>Here is the thread return code... it's all in the same class</p>
  
  <pre><code>-(void)runFormula:(NSDictionary *)frm
  {
      NSMutableString *formula = [[frm objectForKey:kFormulaExpresion] mutableCopy];
      NSArray *variables = [frm objectForKey:kVariableArray];
      NSArray *evals = [self evaluateVariables:variables];
  
  
         for (NSDictionary *var in evals) {
             NSString *sym = [var objectForKey:kVariableSymbol];
             [formula replaceOccurrencesOfString:sym withString:[[var objectForKey:@"numVal"] stringValue] options:NSCaseInsensitiveSearch range:NSMakeRange(0, [formula length])];
      }
      //parse formula
      //NSLog(@"formula to parse:%@",formula);
      NSNumber *resNo = [formula numberByEvaluatingString];
      // NSLog(@"formula %@ the result : %f",formula,[resNo doubleValue]);
      //NSNumber *resNo = [NSNumber numberWithDouble:result];
      [self performSelectorOnMainThread:@selector(addNewResult:) withObject:resNo waitUntilDone:NO];
  }
  
  #pragma mark -- data aggregation delegate
  -(void)addNewResult:(NSNumber *)nr
  {
       NSLog(@"index : %i result: %f",currentIndex,[nr doubleValue]);
      [[self delegate] didReceiveResult:nr];
      resultsArray[currentIndex]=[nr doubleValue];
      currentIndex ++;
      if (  (currentIndex % percentVal) == 0) {
          [[self delegate] percentCompleted];
      }
  }
  </code></pre>