Re: Bindings and Core Data question.
Re: Bindings and Core Data question.
- Subject: Re: Bindings and Core Data question.
- From: email@hidden
- Date: Wed, 12 Oct 2005 19:23:45 -0700
OK, after looking at your code again, what your not doing:
- (void)setB:(NSManagedObject *)value
{
Here you need to remove yourself as an observer of old "b"
[self willChangeValueForKey: @"b"];
[self setPrimitiveValue: value
forKey: @"b"];
[self didChangeValueForKey: @"b"];
Now you are free to register for the new one.
[self addObserver:[self b]
forKeyPath:@"distance"
options:(NSKeyValueObservingOptionNew)
context:NULL];
You need to pass a constant value for the context so you know its
only for exactly what you want, just a string comparison is not enough.
---------------------------------------------------
Here's how I see it, maybe I'm wrong.
A -> B (To one)
A <<-- B (To Many)
B -> observes -->> A.distance (many)
notify B <- change <- A.distance
Right?
Then call addObserver:self only from B ( in setA:), since B is the
observer, A doesn't need to know about its own change or even B,
I think you called method to the wrong class. It should be in B since
it needs to "observeValueForKeyPath" of A.distance
therefore call addObserver in B class not A... at least thats what
your first post implied.
setA:
B -> addObserver -> A
B <- observeValue <- A.distance changed
setTotalDistance:
Since B observes the A's it needs to implement observe Value and also
adds itself as an observer,
your adding the relationship from the wrong end (A) IMHO.
===========================
const NSString *CONTEXT_FOR_DISTANCE_CHANGE = @"DistanceChangeContext";
- (void)addAObject:(NSManagedObject *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value
count:1];
[self willChangeValueForKey:@"a"
withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey: @"a"] addObject: value];
[self didChangeValueForKey:@"a"
withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
//you may wanna check here first to also make sure your not re-
adding incase value was something you already had added before
[value addObserver:self forKeyPath:@"distance" options:
(NSKeyValueObservingOptionNew) context:CONTEXT_FOR_DISTANCE_CHANGE];
}
- (void)removeAObject:(NSManagedObject *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value
count:1];
[self willChangeValueForKey:@"a"
withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey: @"a"] removeObject: value];
[self didChangeValueForKey:@"a"
withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
[value removeObserver:self forKeyPath:@"distance" ];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
if(context == CONTEXT_FOR_DISTANCE_CHANGE)
{
NSLog(@"receiving notification that the distance changed");
[self updateTotalDistance];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
============================
See if that works
_______________________________________________
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