假设我在 Core Data 中有一个 House 实体,它与许多 GraphicalAssets 相关联,它们是 House 的图片。
我想彻底清除所有房屋以及房屋本身的所有图形资产。对于两个方向,关系上的删除行为是 Nullify。删除东西的正确顺序似乎是先删除图形资产,然后是房屋。这是发生的事情:
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?
所以,看起来,因为房屋曾经指向图形资产(这种关系在资产被删除时应该被取消),当我删除房屋时它神奇地复活了被删除的对象。为什么会这样?我什至尝试在删除任何一个对象之前,在两个方向上明确地消除房屋与其图形资产之间的关系,它仍然以这种方式运行。