Re: Core Data to many relationship deletion causing exception
Re: Core Data to many relationship deletion causing exception
- Subject: Re: Core Data to many relationship deletion causing exception
- From: Keary Suska <email@hidden>
- Date: Tue, 16 Aug 2011 13:45:27 -0600
On Aug 16, 2011, at 12:18 PM, Andrew Kinnie wrote:
> I have an iOS 4 + app, which is now being retrofitted to use Core Data. I have an Entity "Article" which has a to-many relationship to another Entity "MediaResource" and I generated NSManagedObject subclasses for each. The relationship is called "media" and is set to be optional, and to Cascade delete the associated MediaResource objects. There is an inverse to-one back to the Article (with a delete rule of nullify).
>
> The generated code included a property of type NSSet * media in the Article class, as well as (among others) these methods:
Who generated the code? Xcode? Or Something else? Either there is a bug in the generated code or the code is expecting you provide mutable backing for the declared property. I would file a bug anyway as the code should generate those methods as well.
> - (void)addMediaObject:(MediaResource *)value {
> NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
> [self willChangeValueForKey:@"media" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
> [[self primitiveValueForKey:@"media"] addObject:value];
> [self didChangeValueForKey:@"media" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
> [changedObjects release];
> }
>
> - (void)removeMediaObject:(MediaResource *)value {
> NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
> [self willChangeValueForKey:@"media" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
> [[self primitiveValueForKey:@"media"] removeObject:value];
> [self didChangeValueForKey:@"media" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
> [changedObjects release];
> }
>
> There are no NSMutableSets here anywhere, but I assume the auto-generated code knows what it is doing. I can add an article, then add a MediaResource object to it, and do it like this:
Clearly, not a safe assumption, or at least it isn't safe to assume that you have provided the "generator" of the code enough information to generate proper code. Either could be true, so the best bet is to always understand exactly what any code is doing. Or, as you see, you won't be able to debug it.
In short, you either need to implement a mutable ivar-backed primitive value or use different methods for manipulating your sets.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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