setPrimitiveValue:forKey: and to-many relationships
setPrimitiveValue:forKey: and to-many relationships
- Subject: setPrimitiveValue:forKey: and to-many relationships
- From: "Sean McBride" <email@hidden>
- Date: Wed, 24 Sep 2008 15:34:57 -0400
- Organization: Rogue Research
Hi all,
Apple's documentation[1] on -setPrimitiveValue:forKey: is vague in two
ways when using it to manage to-many relationships.
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?
Second, providing sample code to correctly handle this case, they write:
"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)?
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?
Thanks,
[1] <http://developer.apple.com/documentation/Cocoa/Reference/
CoreDataFramework/Classes/NSManagedObject_Class/Reference/
NSManagedObject.html#//apple_ref/occ/instm/NSManagedObject/
setPrimitiveValue:forKey:>
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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