• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Bindings and Core Data question.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Bindings and Core Data question. (From: Carlos Rivera <email@hidden>)
 >Re: Bindings and Core Data question. (From: Carlos Rivera <email@hidden>)

  • Prev by Date: Getting copy and paste with custom text attributes to work
  • Next by Date: Re: How to load from templates like in iWork using CoreData?
  • Previous by thread: Re: Bindings and Core Data question.
  • Next by thread: Re: Bindings and Core Data question.
  • Index(es):
    • Date
    • Thread