Search code examples
ioscore-datansmanagedobjectcontextkey-value-observing

key value observing vs. NSManagedObjectContextObjectsDidChangeNotification


I am working on a shopping list app, and I have a couple of cases where I need to watch for changes to an entity, and respond by making changes to other entities. For example:

When a new store entity is added, I need to create related aisle entities.

When the quantity is changed on a food item to 0 or >0, I need to set a related state flag on the item (ftr, the flag has more than two states, I can't just drop the flag and test for 0).

Based on what I've read so far, the choices seem to be between using key-value observing & registering for a NSManagedObjectContextObjectsDidChangeNotification. What I'm not sure of is which is more suited to my scenarios (or if I should just override the appropriate methods in the related entity classes). Any pointers and/or documentation on when to use these would be much appreciated.


Solution

  • If the flag is a sort of derived property from the quantity information, then your best approach might be to override the setter for the quantity in your managed object subclass and update the flag there. Since the flag is a model property, it makes sense to keep responsibility for its value in the model, rather than in a view controller or other non-model object that's using KVO or responding to notifications.