Dependent value on to-many Core Data relationship
Dependent value on to-many Core Data relationship
- Subject: Dependent value on to-many Core Data relationship
- From: Leonardo Cassarani <email@hidden>
- Date: Tue, 12 Feb 2008 19:25:15 +0100
Hi all
In the "Model Object Implementation Guide" (available at http://devworld.apple.com/documentation/Cocoa/Conceptual/ModelObjects/ModelObjects.pdf)
is the following note:
"Important: Note that you cannot set up dependencies on to-many
relationships. For example, suppose
you have an Order object with a to-many relationship (orderItems) to a
collection of OrderItem
objects, and OrderItem objects have a price attribute. You might want
the Order object have a
totalPrice attribute that is dependent upon the prices of all the
OrderItem objects in the relationship.
You can not do this by implementing
keyPathsForValuesAffectingValueForKey: and returning
orderItems.price as the keypath for totalPrice. You must observe the
price attribute of each of
the OrderItem objects in the orderItems collection and respond to
changes in their values by updating
totalPrice yourself. "
This is almost identical to the problem I'm having. I've subclassed
NSManagedObject and I need to set its "total" value to the sum of the
"sum" values in a collection of managed objects with which my main MO
has a to-many relationship. Those values will often be changed, so I
need to update the "total" value every single time.
I'm a beginner at Core Data and Cocoa in general and haven't quite
understood how am I supposed to "observe the price attribute of each
of the OrderItem objects in the orderItems collection and [...]",
despite my going through the KVO documentation multiple times. I'd
really appreciate any help on this.
Thanks a lot.
Leonardo
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden