Re: Dependent value on to-many Core Data relationship
Re: Dependent value on to-many Core Data relationship
- Subject: Re: Dependent value on to-many Core Data relationship
- From: Ken Thomases <email@hidden>
- Date: Wed, 13 Feb 2008 16:36:16 -0600
On Feb 12, 2008, at 12:25 PM, Leonardo Cassarani wrote:
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.
Your class can observe its own orderItems property. As elements are
inserted, removed, and replaced, you'll receive notification of that
via observeValueForKeyPath:ofObject:change:context:. When an element
is added to the collection, initiate observing its "price" key and add
its price into your totalPrice. When it's removed, stop observing it
and subtract its price from your totalPrice. When an element's
"price" changes, you'll receive a notification and you can recalculate
your totalPrice.
One problem is how to start observing all of your initial orderItems,
and how to calculate your initial total. If you're targeting Leopard,
you can pass NSKeyValueObservingOptionInitial in the options when you
first start observing your own orderItems property. Otherwise, you'll
need to make one pass through your orderItems manually and start
observing them all, and accumulate all of their prices into your
initial totalPrice.
-Ken
_______________________________________________
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