Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core data: [NSFetchedResultsController -fetchedObjects], will this method reflect object change?

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.

like image 854
CarmeloS Avatar asked Nov 15 '12 05:11

CarmeloS


2 Answers

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.

like image 163
Martin R Avatar answered Sep 29 '22 07:09

Martin R


@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.

like image 34
Maciek Czarnik Avatar answered Sep 29 '22 09:09

Maciek Czarnik