Re: setPrimitiveValue:forKey: and to-many relationships
Re: setPrimitiveValue:forKey: and to-many relationships
- Subject: Re: setPrimitiveValue:forKey: and to-many relationships
- From: Quincey Morris <email@hidden>
- Date: Wed, 24 Sep 2008 15:56:36 -0700
On Sep 24, 2008, at 12:34, Sean McBride wrote:
First they state:
"If you try to set a to-many relationship to a new NSMutableSet
object,
it will (eventually) fail."
Eventually?! What does that even mean? Will it fail later during -
[NSManagedObjectContext save:]? When an managed object is turned
into a
fault and then paged back in? When? Can I write a test case to
consistently recreate the failure on-demand?
Why would you do that? The documentation says you must NOT call
setPrimitiveValue with an arbitrary set, if the property is a to-many
relationship.
Second, providing sample code to correctly handle this case, they
write:
The sample code isn't handling this case. It's showing you how to
avoid this case (how to create a set that you can validly use with
setPrimitiveValue).
"first get the existing set using primitiveValueForKey: (ensure the
method does not return nil)..."
What should I do if/when the method does return nil? assert() it and
fail immediately because that means the entire object graph is
corrupted
and saving will lead to data loss? NSAssert() on it as a warning to
the
caller but press on (silently doing nothing)?
primitiveValueForKey might fail and return nil; mutableCopy might fail
and return nil; your method might do other things that can fail. Since
there's no error return, there's not a lot you can do. Either log an
error and exit early, or throw an exception.
Right now I'm simply directly assigning my desired NS[Mutable]Set in
that case, like so:
- (void)setChildren:(NSSet*)value_
{
[self willChangeValueForKey:@"children"];
NSMutableSet *mutableRelationshipSet = [[[self
primitiveValueForKey:@"children"] mutableCopy] autorelease];
if (mutableRelationshipSet) {
[mutableRelationshipSet setSet:value_];
[self setPrimitiveValue:mutableRelationshipSet
forKey:@"children"];
} else {
[self setPrimitiveValue:value_ forKey:@"children"];
}
[self didChangeValueForKey:@"children"];
}
Is that wrong?
I think so. Your "else" statement does what the documentation tells
you not to do.
IAC, it's not clear why you need to use setPrimitiveValue: at all. Why
not something like:
- (void)setChildren:(NSSet*)value_
{
[[self mutableSetValueForKey:@"children"] removeAllObjects];
[[self mutableSetValueForKey:@"children"] unionSet: value_];
}
or:
@dynamic addChildren;
@dynamic removeChildren;
- (void)setChildren:(NSSet*)value_
{
[self removeChildren: [NSSet setWithSet: self.children]];
[self addChildren: value_];
}
both of which have the advantage of maintaining the inverse
relationship properly, which your original (according to the
documentation) does not.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden