Re: Bindings and Core Data question.
Re: Bindings and Core Data question.
- Subject: Re: Bindings and Core Data question.
- From: Carlos Rivera <email@hidden>
- Date: Wed, 12 Oct 2005 20:38:16 -0400
I just added the code to do that. No improvement. here is what I added:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
NSLog(@"receiving notification that the distance changed for a Car");
if([keyPath isEqual:@"distance"])
{
NSLog(@"receiving notification that the distance changed for a Trip");
[self updateTotalDistance];
}
NSLog(@"Receiving notification that SOMETHING ELSE changed ");
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
As for the recommendation for using setKeys:
triggerChangeNotificationsForDependentKey: ..., I am not sure (the
again I am a Java switcher/Cocoa beginner). The documentation in the
"Core Data Programming Guide", to be specific in the "Core Data FAQ"
says the following about this situation:
I have a key whose value is dependent on values of attributes in a
related entity—how do I ensure it is kept up to date as the attribute
values are changes and as the relationship is manipulated?
It is not possible to use a key-path in
setKeys:triggerChangeNotificationsForDependentKey:. For example, if
you have an entity Department with a to-many relationship (employees)
to Employee, and Department has a derived attribute (totalSalary) that
is dependent on an attribute (salary) of Employee, then the following
code (in a custom class for Department) will not work:
+ (initialize) {
NSArray *keys = [NSArray arrayWithObject:@"employees.salary"];
[self setKeys:keys
triggerChangeNotificationsForDependentKey:@"totalSalary"];
}
In many respects this is a key-value observing, not a Core Data,
issue, however with Core Data there are two possible solutions.
You can use key-value observing to register the parent (in this
example, Department) as an observer of the relevant attribute of all
the children (Employees in this example). You must add and remove the
parent as an observer as child objects are added to and removed from
the relationship. You then implement an
observeValueForKeyPath:ofObject:change:context: method to update the
dependent value in response to changes, as illustrated in the
following code fragment:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (context == totalSalaryContext) {
[self updateTotalSalary];
}
else
// deal with other observations and/or invoke super...
}
- (void)updateTotalSalary
{
[self setTotalSalary:[self valueForKeyPath:@"email@hiddeny"]];
}
- (void)setTotalSalary:(NSNumber *)newTotalSalary
{
if (totalSalary != newTotalSalary) {
[self willChangevalueForKey:@"totalSalary"];
[totalSalary release];
totalSalary = [newTotalSalary retain];
[self didChangevalueForKey:@"totalSalary"];
}
}
- (NSNumber *)totalSalary
{
return totalSalary;
}
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.
On 10/12/05, Andre <email@hidden> wrote:
> Just a guess, but maybe you should call the method [super
> observeValueForKeyPath...] after
> or before you perform your custom code?
>
> I think what you want to do is in initialize method of the class, set
> a dependent key for
> distance that would also trigger totalDistance... its described in
> the coredata guide:
> + (void)setKeys:(NSArray *)keys
> triggerChangeNotificationsForDependentKey:(NSString *)dependentKey
>
> where keys is and array with the key @"distance" and maybe also "b"
> and the dependent key @"totalDistance" so when
> distance is updated, totalDistance is automatically called, so in
> your totalDistance method, you just do the
> calculation and return it automatically. In this case you dont need
> to subclass observeValueForKeyPath:
>
> > [self addObserver:[self b]
> Since your calling self to observe something from yourself, you
> should also call super from your subclass
> implementation. Or just use setKeys:
> triggerChangeNotificationsForDependentKey: in + initialize.
> Don't call super from +initialize.
>
> On 17/10/07, at 17:47, Carlos Rivera wrote:
>
> > - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
> > change:(NSDictionary *)change context:(void *)context
> > {
> >
> > if([keyPath isEqual:@"distance"])
> > {
> > NSLog(@"receiving notification that the distance changed");
> > [self updateTotalDistance];
> > }
>
> [super observeValueForKeyPath: keyPath ofObject: object
> change : change context: context ];
>
> > }
> >
>
>
--
@@@@@@@@@@@@@@@@@@@@
riverawynter?gmail.com
http://www.riverawynter.org
@@@@@@@@@@@@@@@@@@@@
_______________________________________________
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