Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top