gpt4 book ai didi

objective-c - 为什么当我删除相关对象时,Core Data 会复活已删除的对象?

转载 作者:行者123 更新时间:2023-11-28 22:57:11 25 4
gpt4 key购买 nike

假设我在 Core Data 中有一个 House 实体,它与许多 GraphicalAssets 相关联,这些 GraphicalAssets 是 House 的图片。

我想彻底清除所有房屋的所有图形 Assets ,以及房屋本身。关系的删除行为,对于两个方向,都是 Nullify。删除东西的正确顺序似乎是先删除图形 Assets ,然后再删除房屋。这是发生了什么:

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?

所以,看起来,因为房子曾经指向图形 Assets (删除 Assets 时应该取消的关系),所以当我删除房子时它神奇地复活了删除的对象。为什么会这样?在删除任何一个对象之前,我什至尝试在两个方向上明确取消房屋与其图形 Assets 之间的关系,并且它仍然以这种方式运行。

最佳答案

HouseGraphic 的对多关系的删除规则应该是级联的 - 这意味着删除 House 会将删除级联到所有 Graphic 对象

关于objective-c - 为什么当我删除相关对象时,Core Data 会复活已删除的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com