Re: Core Data to many relationship deletion causing exception - SOLVED?
Re: Core Data to many relationship deletion causing exception - SOLVED?
- Subject: Re: Core Data to many relationship deletion causing exception - SOLVED?
- From: Andrew Kinnie <email@hidden>
- Date: Tue, 16 Aug 2011 16:14:11 -0400
Hmm. Well the generated code was created by Xcode 4's "Create NSManagedObject Subclass" based on the model. The properties are created along with the methods, however due to an earlier issue, I changed @dynamic to @synthesize for the various properties in the .m for Article and MediaResource. There are no ivars. Apparently this caused the problem. Changing it back to @dynamic fixed it.
Further debugging indicated that when I deleted an article, using
[self.moc deleteObject:myArticle];
and at the point that is called, the article had a media object in it's media relationship (doing po myArticle in the debugger indicates this) but when I click continue, it lands on a breakpoint in the removeMediaObject method in Article, and the value passed in exists, but the media relationship itself contains no objects. That is where the crash occurred. So apparently, Core Data was doing the cascade delete, then because I had "@synthesize" instead of "@dynamic" it called the class's removeMediaObject method trying to remove the object that is no longer in the relationship, thus it breaks by calling removeObject" on the __NSSet0 singleton object.
Thanks
On Aug 16, 2011, at 3:45 PM, Keary Suska wrote:
> 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