Search code examples
iosobjective-ccore-datansfetchedresultscontroller

Core Data & NSFetchedResultsController: ignoring fields in controllerDidChangeContent


I have a background thread that performs background updates, sets/overwrites fields on several kinds of NSManagedObjects. All subclasses include a lastModified attribute which always gets updated when being processed by the background task.

My problem is that when I do this, all entities that are hit by the background thread will also trigger controllerWillChangeContent/controllerDidChangeContent. Even if nothing really changed.

  • If I hit a setter (for example with an NSString but the contents didn't actually change, will it still trigger an update or is CoreDate clever enough to ignore this?

  • Is it possible to somehow mark an attribute to not be included when notifying changes to resultsControllers?


Solution

  • You can use setPrimitiveValue:forKey: to change a NSManagedObject without sending any KVO notifications (which trigger the nsfetchedresultscontroller to update)

    for example

    [self setPrimitiveValue:[NSDate date] forKey:@"lastModified"]
    

    You can read more about these here https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154-SW5