Reputation: 6699
Let's say I have a House entity in Core Data, and it is associated with many GraphicalAssets, which are pictures of the House.
I want to totally blow away all graphical assets for all houses, and the houses themselves. The delete behavior on the relationship, for both directions, is Nullify. It seems like the correct order to delete things is to blow away the graphical assets first, then the houses. Here's what happens:
NSFetchRequest* fetchAllGraphicalAssets = [ [ NSFetchRequest alloc ] initWithEntityName:@"GraphicalAsset" ];
NSArray* allGraphicalAssets = [ self.managedObjectContext executeFetchRequest:fetchAllGraphicalAssets error:nil ];
for( GraphicalAsset* asset in allGraphicalAssets )
[ self.managedObjectContext deleteObject:asset ];
[ self.managedObjectContext processPendingChanges ];
// At this point, if I execute fetchAllGraphicalAssets again, the count is 0. Perfect.
NSFetchRequest* housesRequest = [ NSFetchRequest fetchRequestWithEntityName:@"House"];
NSArray* existingHouses = [self.managedObjectContext executeFetchRequest: housesRequest error:nil];
for( House* house in existingHouses )
[ self.managedObjectContext deleteObject: house ];
[ self.managedObjectContext processPendingChanges ];
// Now, if I execute fetchAllGraphicalAssets again, the count is > 0. What is going on?
So, it seems like, because houses used to point to the graphical assets (a relationship that should have been nullified when the assets were deleted), it magically resurrected the deleted objects when I delete the houses. Why is this happening? I have even tried explicitly nulling out the relationship between the houses and their graphical assets, in both directions, before deleting either object, and it still behaves this way.
Upvotes: 0
Views: 186
Reputation: 23359
Delete rule for the to-many relationship from House
to Graphic
should be cascade - this means deleting a House
will cascade the delete to all the Graphic
objects
Upvotes: 2