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: Thu, 23 Feb 2006 10:10:54 +0100
> Calling the RemoveObject routine twice would only be problematic if it were
> to be called twice on the same object. It doesn't sound like we know whether
> it's one or two object instances involved. So I would suggest amending the
> NSLog call, perhaps with: NSLog(@"removing object for: %@", self). And then
> I would compare the two entries in the log to find out whether the two are
> in fact the same instance.
Good point greg,
I tested it and it's really called twice for the same object. The
remove<Key>Object looks like this:
-(void)removeBooksObject:(NSManagedObject *)value {...} thus it's easy
to test it with a simple NSLog(@"removed book: %@", value);
> And is there a particular reason to have the application implement these
> routines...
Normally, no. But what we are trying to achieve here is to implement
the scenario described in the Core Data FAQ (in the section "I have a
key whose value is dependent...."), and what needs to be done in this
case is adding and removing the parent (author) as an observer as
child objects (books) are added to and removed from the relationship.
Consider the sample app I described in my previous email, imagine we
add a new attribute "price" to the Book entity and a new attribute
"earnings" to the Author entity that represents the total net income
the author earns when selling his books. That attribute is obviously
derived from some calculation based on the price of each book, and we
want it to be updated as soon as the price changes.
That's why we need those routines: to add KVO to register the author
as an observer of the price attribute of all his books...
Now everything works, but when I remove a book the -removeBooksObject:
is called twice, hence [value removeObserver:self forKeyPath:@"price"]
is called twice and the app crashes.
_______________________________________________
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