title: Why my completionBlock never gets called in an NSOperation?
body: <p>I've sublcassed an NSOperation and set my completionBlock but it seems to never enter even when the operation finishes. Here's my code:</p>
  
  <p>A catalog controller class sets up the NSOperation:</p>
  
  <pre><code>- (void)setupOperation {
  ...
  
      ImportWordOperation *importWordOperation = [[ImportWordOperation alloc] initWithCatalog:words];
      [importWordOperation setMainObjectContext:[app managedObjectContext]];
      [importWordOperation setCompletionBlock:^{
          [(ViewController *)[[app window] rootViewController] fetchResults];
      }];
      [[NSOperationQueue mainQueue] addOperation:importWordOperation];
      [importWordOperation release];
  ...
  }
  </code></pre>
  
  <p>As you can see, I'm setting the completion block to execute a method on the main thread, in some other controller.</p>
  
  <p>Then, in <code>main</code> my subclassed NSOperation class: <code>ImportWordOperation.m</code>, I fire-up the background operation. I even overrode <code>isFinished</code> iVar in order for the completion method to be triggered:</p>
  
  <pre><code>- (void)setFinished:(BOOL)_finished {
      finished = _finished;
  }
  
  - (BOOL)isFinished {
      return (self.isCancelled ? YES: finished);
  }
  
  - (void)addWords:(NSDictionary *)userInfo {
      NSError *error = nil;
  
      AppDelegate *app = [AppDelegate sharedInstance];
  
      NSManagedObjectContext *localMOC = [userInfo valueForKey:@"localMOC"];
      NSEntityDescription *ent = [NSEntityDescription entityForName:@"Word" inManagedObjectContext:localMOC];
      for (NSDictionary *dictWord in [userInfo objectForKey:@"words"]) {
          Word *wordN = [[Word alloc] initWithEntity:ent insertIntoManagedObjectContext:localMOC];
  
          [wordN setValuesForKeysWithDictionary:dictWord];
          [wordN release];
      }
  
      if (![[userInfo valueForKey:@"localMOC"] save:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      }
      [localMOC reset];
  
      [self setFinished:YES];
  }
  
  
  - (void)main {
  
      finished = NO;
  
      NSManagedObjectContext *localMOC = nil;
      NSUInteger type = NSConfinementConcurrencyType;
      localMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:type];
      [localMOC setUndoManager:nil];
      [localMOC setParentContext:[self mainObjectContext]];
  
      if (![self isCancelled]) {
          if ([self.words count] &gt; 0) {
              [self performSelectorInBackground:@selector(addWords:) withObject:@{@"words":self.words, @"localMOC":localMOC}];
          }
      }
  }
  </code></pre>
  
  <p>If I remove isFinished accessor methods, then the completion block gets called but way before <code>ImportWordOperation</code> finishes.</p>
  
  <p>I've read code that I've found that uses its own completion block but then what's the use for the completion block in NSOperation subclasses anyway?</p>
  
  <p>Any ideas or point to a similar solved situation would be greatly appreciated.</p>