Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...