Since my previous post, I have been able to get the functionality I
was hoping for with the following code in my "Department" subclass. I
would greatly appreciate some feedback as to whether this is an
appropriate way to implement the functionality or if there is a more
efficient or cleaner way.

- (void)awakeFromFetch {
    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContextDidChange:) name:
NSManagedObjectContextObjectsDidChangeNotification object:[self

- (void)awakeFromInsert {
    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContextDidChange:) name:
NSManagedObjectContextObjectsDidChangeNotification object:[self

- (void)managedObjectContextDidChange:(NSNotification *)notification {
    // Get a set containing ALL objects which have been changed
    NSSet *insertedObjects = [[notification userInfo]
    NSSet *updatedObjects = [[notification userInfo]
    NSSet *deletedObjects = [[notification userInfo]
//this method of gathering changed objects is because the final set
was always null if the first set was null
    NSSet *changedObjects;
        if([insertedObjects count] > 0){
                changedObjects = [insertedObjects 
                changedObjects = [changedObjects 
                if([updatedObjects count] > 0){
                        changedObjects = [updatedObjects 
                        changedObjects = [NSSet setWithSet:deletedObjects];

        if([changedObjects intersectsSet:[self employees]]){
                //if an employee in this department changed, indicate
that the totalSalary attribute should be refreshed
                [self willChangeValueForKey:@"totalSalary"];
                [self didChangeValueForKey:@"totalSalary"];

Thank you,


On Mon, Oct 5, 2009 at 1:18 PM, John McIntosh
<> wrote:
> Hi all.
> I am working on a Core Data application which contains an entity that
> needs to be notified of changes in entities that it has a relationship
> to. Let's talk about this in terms of Apple's example with Departments
> and Employees. As described here
> (
> they talk about the example where each Department has a "totalSalary"
> attribute and each Employee has a "salary" attribute.
> In regards to Core Data, the documentation linked above says this:
> "If you're using Core Data, you can register the parent with the
> application's notification center as an observer of its managed object
> context. The parent should respond to relevant change notifications
> posted by the children in a manner similar to that for key-value
> observing."
> I assume this means that something similar to the code posted
> previously in the article should be done. I understand the statement
> conceptually, but do not know how to go about implementing it. Here is
> my start in the Department entity.
> - (void) awakeFromInsert{
>     [[NSNotificationCenter defaultCenter]    addObserver:self
> selector:@selector(methodToCallOnNotification:)   name:???
> object:managedObjectContext];
> }
> If this is the correct start, what should happen in 
> methodToCallOnNotification?
> Do I need to remove each Department entity from being an observer when
> it is deleted?
> What else do I need to be sure to do upon Department or Employee
> creation or deletion?
> Thank you,
> John

Cocoa-dev mailing list (

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to

Reply via email to