Does NSFetchedResultsController Observe All Changes to Persistent Store?
Asked Answered
L

2

7

My program does work like link below:

Update results of NSFetchedResultsController without a new fetch

  1. show result of NSFetchedResultsController to UITableView
  2. get new object from web service and store it to core data (in same view controller, with RestKit)
  3. update table view with notification of NSFetchedResultsController delegate

The implementation of NSFetchedResultsControllerDelegate is copied from Apple's Core Data project and my predicated is:

[NSPredicate predicateWithFormat:@"isMyTest == TRUE"]

If the property update goes from TRUE to FALSE, it removes rows from the tableview (because the object for the row is in fetchedObjects of the NSFetchedResultsController)

However, if the property update goes from FALSE to TRUE, the NSFetchedResultsController notifies nothing, so the new data cannot be seen in table view. If I update BOTH NSFetchedResulsController and UITableView manually, it shows the new data.

I thought NSFetchedResultController watches all changes in persistent store, is it too big hope? :D

(I really want to do that because other view controller can update persistent store, then it is hard to update the view controller.)

If so, can you let me know how can I update NSFetchedResultsController in beautifully way?

(update)

in reference of NSfetchedResultsController, I read words below:

A controller thus effectively has three modes of operation, determined by whether it has a delegate and whether the cache file name is set.

No tracking: the delegate is set to nil. The controller simply provides access to the data as it was when the fetch was executed.

Memory-only tracking: the delegate is non-nil and the file cache name is set to nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes.

Full persistent tracking: the delegate and the file cache name are non-nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes. The controller maintains a persistent cache of the results of its computation.

"Full persistent tracking" does not mean what I want? I set up cacheName, but it works same.

Landan answered 2/9, 2011 at 8:17 Comment(0)
G
5

I thought NSFetchedResultController watches all changes in persistent store, is it too big hope?

The FRC only automatically observes the objects returned by its fetch. This is normally not a problem as changes to objects monitored by an FRC should arise from either the tableview UI or a background context. In the former, the FRC is alerted to the change and in the latter, merging the foreground and background context will trigger the FRC to update.

It sounds like you've got code changing values but you're not notifying the FRC that you've made any changes. (If you got a tableview displaying all objects whose isMyTest == TRUE then obviously you can't access objects from the UI whose isMyTest == FALSE.) In that case, you need to register the tableview controller for:

NSManagedObjectContextObjectsDidChangeNotification

… notifications from the context so that you can tell the FRC to update for changes made outside its observation.

Guiana answered 2/9, 2011 at 16:18 Comment(2)
Thank you very much to your answer and edit of my question, but I found CoreData DOES full repository tracking" with testing by making new test project.Landan
I'm debugging my project if there's my mistake.Landan
L
0

I'm sorry to my mistake, I tested with new test project.

CoreData DOES full-tracking of whole persistent store.

This means, if new object that is suitable to predicate of NSFetchedResultsController, delegate will notify it.

Landan answered 5/9, 2011 at 9:59 Comment(4)
No, the context notifies the FRC because the FRC has registered with the context to receive insert,update and delete notifications. If you make a change not known to the FRC context's is will not track it. E.g. a context on another thread makes a change that is not merged with the FRC's context. The FRC is tracking the context NOT the persistent store.Guiana
Ah, thx. You mean changes in NOT whole persistent store, but in WHOLE data context, I got it.Landan
My problem was, change of object that is not in result of FRC, is not notified to delegate.Landan
Any way to tell the FRC to observe nested properties of the managed object also?Gagarin

© 2022 - 2024 — McMap. All rights reserved.