开发者

When is an NSFetchedResultsController updated?

开发者 https://www.devze.com 2023-04-11 23:08 出处:网络
I am using an NSFetchedResultsController that uses a predicate to fetch some managed objects. I have always assumed that the NSFetchedResultsController is updated when the开发者_StackOverflow中文版 ob

I am using an NSFetchedResultsController that uses a predicate to fetch some managed objects. I have always assumed that the NSFetchedResultsController is updated when the开发者_StackOverflow中文版 objects change, i.e. if I have a predicate "box == 3" and box is changed to 2, it won't be fetched.

However, even though I changed the objects and saved the managed object context, I can still access the object through my FRC. Even when deleting the cache, this is still possible.

Is this the correct behavior?


Did you set a delegate for the NSFetchedResultsController? When changes occur in the managed object context, the NSFetchedResultsController's delegate is the one that is notified of the changes. Please see the following:

http://developer.apple.com/library/ios/#DOCUMENTATION/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference.html


A simple way to e.g. update your table is just to set the FRC to nil and reload. That tends to take care of all the updating problems and is also quite efficient. The FRC will lazily recreate itself and populate your table.

self.fetchedResultsController = nil;
[self.tableView reloadData];


After some further testing I think it is save to assume that the FRC is not updated without me doing something. I only call -performFetch: once and I did not implement the delegate. According to the docs the FRC seems to only then register for change notifications:

If you set a delegate for a fetched results controller, the controller registers to receive change notifications from its managed object context.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号