1

Is there a way to have changes to an object's attribute also "alert" related objects?

The structure we have is as follows:

  • Image has an attribute called content
  • Category has a one-to-one relationship to Image

It would be ideal if changes to attributes within the Image object could be detected by the related Category, in a way that the Category would be included in the NSUpdatedObjectsKey of NSManagedObjectContextObjectsDidChangeNotification. I've seen some suggestions indicating that adding a sentinel attribute such as needsUpdate to Category would be a good way to do this, but that seems like a cumbersome way of handling this.

My reasoning for doing this is that I need to reload a tableview whenever a Category changes, or whenever it's associated Image changes, at the moment in my observation method for NSManagedObjectContextObjectsDidChangeNotification I check updated/deleted/inserted objects for Image instances or Category instances, however Image instances are used elsewhere in the app and may have no relationship to a Category instance, in which case it would be a waste to reload the tableview. I could manually loop through the updated/deleted/inserted objects to see if they are Image instances associated with a Category, but that doesn't seem like the best place to do it.

I found that this question is similar to what I am attempting, however it has no answer.

Please let me know if additional information is needed, or if my question is too convoluted.

Edit: Modified to hopefully make it more apparent that I'm interested in Category being aware of changes within the Image object's attributes, rather than a change in the relationship itself.

Community
  • 1
  • 1
Arkcann
  • 618
  • 7
  • 15

1 Answers1

0

I would suggest learning about KVO (Key Value Observing) as that framework is designed specifically for this purpose. With KVO you can listen to a specific object for changes on its attributes and then react to them.

However, I question why you are not using a NSFetchedResultsController for this. That is what the NSFetchedResultsController is designed for.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • If I understand the question correctly, it is about tracking changes to related objects. NSFetchedResultsController does not do that on its own, as some master of Core Data told us: http://stackoverflow.com/a/7534013/1187415. – Martin R Jan 27 '14 at 20:21
  • Watching Category and if the Image relationship changes then it would fire. From my read of the OP, the relationship would change, not the data inside of the image object. – Marcus S. Zarra Jan 27 '14 at 20:23
  • I agree that using an `NSFetchedResultsController` likely would have been useful, however as mentioned by @MartinR I don't think it would adequately solve the issue at hand (at least by itself). I don't have a good reason as to why I didn't use `NSFetchedResultsController`, I was likely unfamiliar with CoreData when I originally implemented the tableview (thus did things the hard way) and never found enough reason to refactor it to use an `NSFetchedResultsController` afterwards. – Arkcann Jan 27 '14 at 20:25
  • @MarcusS.Zarra sorry for the ambiguity, but I am interested in detecting a change when the data inside the `Image` object is modified, as opposed to the relationship being changed. I'll update the question to make this more apparent. – Arkcann Jan 27 '14 at 20:27
  • Then in that case my original suggestion stands. You can use KVO to add your view controller as a watcher for that data or you can continue to filter through the `NSManagedObjectContextDidSave` notifications. Personally, based on my experience, filtering through the notifications is cleaner and more maintainable. – Marcus S. Zarra Jan 27 '14 at 20:57
  • @MarcusS.Zarra Ok, so I take it that CoreData isn't able to support this functionality (at least at the moment). Like you suggested, filtering is a good alternative. It might help others if you include that in your original answer, so they don't have to dig through the comments. – Arkcann Jan 28 '14 at 14:43
  • @Arkcann, Core Data is not a single framework/utility. Most of what people consider "Core Data" is in fact the core of Objective-C. Does Core Data support what you want? Yes, through KVO which is part of how Core Data works. To make a statement that it doesn't support the functionality is incorrect because it implies that Core Data is a stand alone framework. – Marcus S. Zarra Jan 28 '14 at 16:21