此答案阐述了 Riley Dutton 在 cmets 中描述的错误消息具有误导性的案例。
当您将不是 NSManagedObject 子类的对象传递给deleteObject: 时,将显示错误消息。 Riley 只是通过显式传入错误的对象来解决问题,但我通过 Core Data 更改解决了问题。
我的项目部署目标设置为 7.0,即使在 iOS 9.3 上运行,此代码也能正常运行:
NSArray *entries = @[self.colorList.colors, self.emotionList.emotions, self.shapeList.shapes];
for (id entry in entries) {
[[self managedObjectContext] deleteObject:entry];
}
当我将项目部署目标更新到 9.3 时,我开始收到错误消息。
这里是entry的描述:
Relationship 'colors' fault on managed object (0x7fd063420310) <MyColorList: 0x7fd063420310> (entity: MyColorList; id: 0xd000000000640006 <x-coredata://12556DEF-F77E-4EFF-AAE6-55E71A3F5420/MyColorList/p25> ; data: {
attachedThing = "0xd0000000000c0004 <x-coredata://12556DEF-F77E-4EFF-AAE6-55E71A3F5420/MyThing/p3>";
colors = "<relationship fault: 0x7fd063468f30 'colors'>";
})
看起来 Apple 更改了 Core Data 何时触发故障并实际从持久存储协调器中提取数据的规则。
这个修改解决了问题:
NSArray *entries = @[self.colorList.colors, self.emotionList.emotions, self.shapeList.shapes];
for (id entry in entries) {
for (id e in entry) {
[[self managedObjectContext] deleteObject:e];
}
}
目前我不知道这是否是解决这个问题的理想方式,或者是否有更规范的方式告诉Core Data触发故障并从磁盘读取数据。