Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NSFetchedResultsController Observe All Changes to Persistent Store?

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.

like image 720
moon6pence Avatar asked Sep 02 '11 08:09

moon6pence


1 Answers

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.

like image 191
TechZen Avatar answered Sep 29 '22 11:09

TechZen