Core Data to many relationship deletion causing exception
Core Data to many relationship deletion causing exception
- Subject: Core Data to many relationship deletion causing exception
- From: Andrew Kinnie <email@hidden>
- Date: Tue, 16 Aug 2011 14:18:30 -0400
Greetings,
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:
- (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:
[newArticle addMediaObject:newMediaObject];
I gather from the docs that is the correct way, however, when we reload the app after saving an article, the media object appears as a fault, then when I say [moc deleteObject:myArticle] and save, it crashes, and breakpoints show the media relationship as being the singleton __NSSet0 object (i.e. an empty non-mutatble set )
As I didn't actually write these methods, I am confused. Any ideas? Here is the relevant stack trace:
2011-08-16 11:12:32.141 MyApp[41825:207] -[__NSSet0 removeObject:]: unrecognized selector sent to instance 0x8041f60
2011-08-16 11:12:32.144 MyApp[41825:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 removeObject:]: unrecognized selector sent to instance 0x8041f60'
*** Call stack at first throw:
(
0 CoreFoundation 0x01a5b5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01baf313 objc_exception_throw + 44
2 CoreFoundation 0x01a5d0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x019cc966 ___forwarding___ + 966
4 CoreFoundation 0x019cc522 _CF_forwarding_prep_0 + 50
5 CTKennedy 0x000b292c -[Article removeMediaObject:] + 220
6 CoreData 0x0054a1f2 -[NSManagedObject(_NSInternalMethods) _excludeObject:fromPropertyWithKey:andIndex:] + 98
7 CoreData 0x0053f7d1 -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 449
8 CoreData 0x00593b55 -[NSManagedObject(_NSInternalMethods) _propagateDelete:] + 1541
9 CoreData 0x0054a02a -[NSManagedObject(_NSInternalMethods) _propagateDelete] + 42
10 CoreData 0x00549e53 -[NSManagedObjectContext(_NSInternalChangeProcessing) _propagateDeletesUsingTable:] + 515
11 CoreData 0x00549c12 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processDeletedObjects:] + 146
12 CoreData 0x0053cba8 -[NSManagedObjectContext(_NSInternalChangeProcessing) _propagatePendingDeletesAtEndOfEvent:] + 104
13 CoreData 0x00508982 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 754
14 CoreData 0x00542715 -[NSManagedObjectContext save:] + 149
_______________________________________________
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