title: Why does / how does controllerDidChangeContent work?
body: <p>I'm coding iOS 6.1 and learning how to use Core Data.</p>
  
  <p>The documentation for fetchedObjects says:</p>
  
  <p>"The results array reflects the in-memory state of managed objects in the controller’s    managed object context, not their state in the persistent store. The returned array does not, however, update as managed objects are inserted, modified, or deleted."</p>
  
  <p>That last sentence bears on what I'm seeing. </p>
  
  <p>I.e., I fetch the data from an entity, I do a count of the fetched objects (fetchedObjects.count) and I get what I expect.  </p>
  
  <p>Then I create a new object and I save it and then I do the fetchedObjects.count again and get the same result as last time - not bumped up by one as I'd expected.</p>
  
  <p>So, my problem was how to get the fetchedObjects to update after I'd saved my context.</p>
  
  <p>I read about using the NSFetchedResultsControllerDelegate <a href="https://stackoverflow.com/questions/13391934/core-data-nsfetchedresultscontroller-fetchedobjects-will-this-method-reflec?rq=1">here</a> and tried out some of what was suggested which was that I use a delegate.</p>
  
  <p>I assigned myself as the delegate.</p>
  
  <pre><code> frc.delegate = self;
  </code></pre>
  
  <p>I added the delegate like so:</p>
  
  <pre><code>@interface DB : NSObject &lt;NSFetchedResultsControllerDelegate&gt;
  </code></pre>
  
  <p>And I implemented the delegate call back like so:</p>
  
  <pre><code>- (void) controllerDidChangeContent: (NSFetchedResultsController *) controller
      {
      ;
      }
  </code></pre>
  
  <p>And it all worked.   But I don't understand why.</p>
  
  <p>If I remove the three bits of delegate code, above, my code fails when I create a new object, save my context and then do the fetchedObjects.count.  It fails by the count coming up one short.</p>
  
  <p>If I add back the NSFetchedResultsControllerDelegate and the frc.delegate = self; bits and leave out the call back routine, it still fails.</p>
  
  <p>Only if I also add the callback routine back in (which does nothing functional, by the way), does my code succeed.</p>
  
  <p>This makes me a bit paranoid - as in wondering why it works if the callback does nothing and if this will work 99% of the time and then fail once in a blue moon?</p>
  
  <p>So, I created a boolean and I set it in the callback routine so my code will not perform the  post-save count until it's seen the boolean flip.  But, I'm thinking this might be redundant - and maybe even silly.</p>
  
  <pre><code>oldCount = frc.fetchedObjects.count;
  &lt;create a new object here&gt;
  frcDelegateCalled = FALSE;   
  [self saveDB];   
  [frc.managedObjectContext processPendingChanges];   
  while ( frcDelegateCalled == FALSE );   
  newCount = frc.fetchedObjects.count;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>- (void) controllerDidChangeContent: (NSFetchedResultsController *) controller
     {
     frcDelegateCalled = TRUE;
     }
  </code></pre>
  
  <p>So, the processPendingChanges call apparently hurries the update along and my boolean makes sure that the callback has been called indicating that the fetchedObjects has been updated before I recount.</p>
  
  <p>I'm a bit confused as to why the code works simply because a callback is present.   Can anyone shed some light here?</p>