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.
Delete rule for the to-many relationship from
HousetoGraphicshould be cascade – this means deleting aHousewill cascade the delete to all theGraphicobjects