6
votes

first I create a NSFetchedResultsController with a NSFetchRequest that fetches all object of entity "Entity".

and then I insert an new NSManagedObject A ,which is an instance of "Entity", and Edit B, which is also an instance of "Entity".

then I call NSArray *result = [fetchedResultsController -fetchedObjects], will A and B in result?

Throughout the project, I use only one NSManagedObjectContext.

if I do, what does this mean in its Document?

fetchedObjects The results of the fetch.

@property (nonatomic, readonly) NSArray *fetchedObjects Discussion The value of the property is nil if performFetch: hasn’t been called.

The results array only includes instances of the entity specified by the fetch request (fetchRequest) and that match its predicate. (If the fetch request has no predicate, then the results array includes all instances of the entity specified by the fetch request.)

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.

3

3 Answers

10
votes

The meaning of the last sentence

... The returned array does not, however, update as managed objects are inserted, modified, or deleted.

is (based on my experiments): If you store a reference to the fetched objects

NSArray *result = [fetchedResultsController fetchedObjects];

then this array referenced by result will not update if objects are inserted, modified or deleted. So the emphasis is on "the returned array does not ...".

But if you call

[fetchedResultsController fetchedObjects]

later again, the return value is a new list of objects, including the changes.

More precisely: If you have set a delegate, the FRC tracks changes to the managed object context and calls the FRC delegate functions. The delegate functions are not called immediately when objects are inserted/modified/deleted, but either

  • when the context is saved explicitly, or
  • when the change notifications are processed in the run loop.

As soon as controllerDidChangeContent: is called, a new call to [fetchedResultsController fetchedObjects] returns the updated object list.

3
votes

@Dwayne use [fetchedResultsController.managedObjectContext processPendingChanges] after your insert/delete/update if you want instant update on your delegate object, normally it is invoked at the end of the runloop.

1
votes

I just did an experiment on this as well. It seems that the fetchedObjects' count number got updated in this call:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath{
}

Dose this mean it's a incremental behavior each time it is called? And have the whole list ready when controllerDidChangeContent: is called since all changes have been executed?

@Maciek Czarnik Thanks for your explanation. However, if we can get the change each time [fetchedResultsController.managedObjectContext processPendingChanges] is not necessary to be called here anymore.

I hope I could have others confirm this bahavior, if possible.

Thanks.