Re: Core Data: updating a key dependent on a to-many relationship?
Re: Core Data: updating a key dependent on a to-many relationship?
- Subject: Re: Core Data: updating a key dependent on a to-many relationship?
- From: zeno <email@hidden>
- Date: Mon, 20 Feb 2006 15:52:06 +0100
> Is there a way to add the observer inside the LineItem object as it's
> initialized (at the same time it sets the inverse relationship to
> Invoice, whenever it does that)? I'm not sure how to implement this.
I don't think you should use addObserver in the LineItem model, I
would rather put it in your Invoice class, since it's the Invoice
object that is supposed to observe a property of LineItem (and then
calculate the subtotal or whatever when that property is modified).
Are you using the mutableSetValueForKey method to actually add a
LineItem object to the Invoice's lineItems (to-many) relationship?
I think you should be able to:
1) right-click on the lineItems relationship defined in the
xcdatamodel diagram and choose "Copy Method Implementation To
Clipboard"
2) Paste it into you Invoice class (NSManagedObject subclass)
3) Call [value addObserver:self
forKeyPath:@"THE_LINEITEM_PROPERTY_YOU_WANT_TO_OBSERVE"......] in the
new addLineItemsObject: method and call [value
removeObserver:self......] in the removeEntriesObject: method.
4) Lastly, implement observeValueForKeyPath:ofObject:change:context:
(always in your Invoice class) as explained in the Core Data FAQ
I think that should do the trick
(not completely sure though, since I didn't test it, but I am actually
implementing something similar for my app...so I'll keep you posted if
it'll work)
---
zeno
_______________________________________________
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