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

I first create an NSFetchedResultsController with an NSFetchRequest that selects the entire Entity entity object.

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

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

Throughout the project, I use only one NSManagedObjectContext.

if I do this, what does this mean in the document ?

fetchedObjects Sampling results.

@property (nonatomic, readonly) NSArray * fetchedObjects Discussion The property value is zero if executeFetch: was not called.

The results array includes only instances of the object specified by the fetchRequest and its corresponding predicate. (If the selection request does not have a predicate, then the result array includes all instances of the object specified in the selection request.)

An array of results reflects the state of the managed objects in memory. The controllers control the context of the object, and not their state in a permanent store. However, the returned array is not updated as managed objects are inserted, modified, or deleted.

+5
source share
3 answers

The meaning of the last sentence

... The returned array, however, is not updated because the managed objects are inserted, modified, or deleted.

(based on my experiments): If you keep a link to extracted objects

 NSArray *result = [fetchedResultsController fetchedObjects]; 

then this array referenced by result will not be updated if objects are inserted, modified, or deleted. Thus, the emphasis is on "array , returned array is not ...".

But if you call

 [fetchedResultsController fetchedObjects] 

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

More precisely: if you set up a delegate, FRC tracks changes in the context of the managed entity and calls the functions of the FRC delegate. The delegate function is not called immediately when objects are inserted / modified / deleted, but either

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

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

+9
source

@Dwayne use [fetchedResultsController.managedObjectContext processPendingChanges] after you insert / delete / update if you want to instantly update your delegate object, usually it is called at the end of runloop.

+3
source

I just did an experiment on this. It looks like the fetchedObjects account number has been updated in this call:

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

Dose does this mean that this is a gradual behavior with every challenge? And is the whole list ready when calling controllerDidChangeContent: since all the changes have been made?

@Maciek Czarnik Thank you for your explanation. However, if we can get changes every time, [fetchedResultsController.managedObjectContext processPendingChanges] no longer needs to be called here.

I hope others can confirm this behavior, if possible.

Thank you

+1
source

Source: https://habr.com/ru/post/1446138/


All Articles