Re: Dependent Keys
Re: Dependent Keys
- Subject: Re: Dependent Keys
- From: Jim Hamilton <email@hidden>
- Date: Wed, 17 Aug 2005 14:30:23 -0400
On Aug 17, 2005, at 1:59 PM, Marcus S. Zarra wrote:
I am trying to set up a dependent key that is dependent on the value
stored inside of a child object. Specifically, assuming I have the
following two objects in Core Data:
Parent<-->>Child
Inside of the child object is a field called "value". In the parent I
want to update a dependent key based on changes to this value. In the
parent's initialize method I have placed the following code:
+ (iniitalize) {
NSArray *keys = [NSArray arrayWithObjects:@"children.value", nil];
[self setKeys:keys
triggerChangeNotificationsForDependentKey:@"dependent"];
}
The issue is that whenever I change the child's value the dependent
key is not being fired/called.
Two questions:
1. Is this possible?
Yes
Can I use the dependent key notification with a keyPath?
No
2. If it is possible, what am I doing wrong?
As you have discovered, what you're doing wrong is trying to use
dependent key notification with a keyPath.
There are two approaches I've looked at:
1) Register Parent for KVO notifications for each Child
2) Register for the notification
NSManagedObjectContextObjectsDidChangeNotification, and filter out
changes that are not a particular Parent's Child instances.
Choice 1 laser-focuses on the problem, but there's a bit of overhead
in registering for KVO notifications (and tearing down those links
when objects go away). I have found that choice 2 works well if
there aren't too many different kinds of inter-entity dependencies,
and it was easy to implement.
You could also probably have Child observe its own 'value', and send
some kind of change notification directly to its Parent. You're
subclassing NSManagedObject anyway, to try to get change
notifications, aren't you? Just add a "childValueChanged" method.
(Of course, this implies that your Child entity needs a subclass as
well, and ties the two classes together.)
hth,
Jim H
--
Jim Hamilton
email@hidden
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden