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
managedObjectContext]];
}

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

- (void)managedObjectContextDidChange:(NSNotification *)notification {
    // Get a set containing ALL objects which have been changed
    NSSet *insertedObjects = [[notification userInfo]
objectForKey:NSInsertedObjectsKey];
    NSSet *updatedObjects = [[notification userInfo]
objectForKey:NSUpdatedObjectsKey];
    NSSet *deletedObjects = [[notification userInfo]
objectForKey:NSDeletedObjectsKey];
        
//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 
setByAddingObjectsFromSet:updatedObjects];
                changedObjects = [changedObjects 
setByAddingObjectsFromSet:deletedObjects];
        }else{
                if([updatedObjects count] > 0){
                        changedObjects = [updatedObjects 
setByAddingObjectsFromSet:deletedObjects];
                }else{
                        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,

John    

On Mon, Oct 5, 2009 at 1:18 PM, John McIntosh
<johntmcint...@googlemail.com> 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
> (http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/KeyValueObserving/Concepts/DependentKeys.html)
> 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 (Cocoa-dev@lists.apple.com)

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

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to