Navigation

NSFetchedResultsController silently ignores changes

Monday, July 18th, 2011

Say you’ve got an NSFetchedResultsController with a predicate like project.enabled == YES. (Imagine this app shows a list of to-do items, organized into projects.) Now say you enable (or disable) a project. What will NSFetchedResultsController do?

I assumed that it would fire insert or delete events for items coming in or out of the set of items that matched the predicate. What actually happens is that NSFetchedResultsController does not pick up these changes.

Perhaps a simpler way of stating it is that NSFetchedResultsController does not seem to track changes when the predicate contains a key path that drills down into a relationship.

Workaround

You can force NSFetchedResultsController to pick up the changes, but it’s a bit ugly. Here’s an example:

[project setValue:[NSNumber numberWithBool:YES] forKey:@"enabled"];

for (NSManagedObject *todo in [project valueForKey:@"todos"]) {     
    [todo willChangeValueForKey:@"project"];
    [todo didChangeValueForKey:@"project"];
}

Posting these “empty” KVO notifications appears to fix the problem.

Radar and sample project

I’ve filed this as radar 9790804. Muchos gracias to ‘Ili Butterfield for whipping up a sample project demonstrating this bug.