【发布时间】:2009-09-23 00:42:38
【问题描述】:
这个问题可能是一个很长的问题。删除实体后保存时,我无法弄清楚我在核心数据项目中遇到的错误。
我有两个主要的实体,一个服装和一个文章。我可以毫无问题地创建它们,但是当我删除它们时,我会收到以下错误日志:
对于服装:
2009-09-22 20:17:37.771 itryiton[29027:20b] Operation could not be completed. (Cocoa error 1600.)
2009-09-22 20:17:37.773 itryiton[29027:20b] {
NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1600.)";
NSValidationErrorKey = outfitArticleViewProperties;
NSValidationErrorObject = <Article: 0x12aa3c0> (entity: Article; id: 0x12b49a0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/Article/p1> ; data: {
articleID = 2009-09-22 19:05:19 -0400;
articleImage = 0x12b4de0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/ArticleImage/p1>;
articleType = nil;
attributeTitles = "(...not nil..)";
color = nil;
comment = nil;
dateCreated = 2009-09-22 19:05:19 -0400;
designer = nil;
imageView = "(...not nil..)";
location = "(...not nil..)";
outfitArticleViewProperties = (
0x12b50f0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/OutfitArticleViewProperties/p1>
);
ownesOrWants = 0;
pattern = nil;
price = nil;
retailer = nil;
thumbnail = "(...not nil..)";
washRequirements = nil;
wearableSeasons = nil;
});
NSValidationErrorValue = {(
<OutfitArticleViewProperties: 0x1215340> (entity: OutfitArticleViewProperties; id: 0x12b50f0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/OutfitArticleViewProperties/p1> ; data: {
article = 0x12b49a0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/Article/p1>;
articleViewPropertiesID = nil;
outfit = nil;
touch = nil;
view = "(...not nil..)";
})
)};
}
如果我删除一篇文章,我会得到:
2009-09-22 18:58:38.591 itryiton[28655:20b] Operation could not be completed. (Cocoa error 1560.)
2009-09-22 18:58:38.593 itryiton[28655:20b] DetailedError: {
NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1600.)";
NSValidationErrorKey = articleImage;
NSValidationErrorObject = <Article: 0x12aa340> (entity: Article; id: 0x12b3f10 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/Article/p1> ; data: {
articleID = 2009-09-22 18:58:26 -0400;
articleImage = 0x12b4d00 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/ArticleImage/p1>;
articleType = nil;
attributeTitles = "(...not nil..)";
color = nil;
comment = nil;
dateCreated = 2009-09-22 18:58:26 -0400;
designer = nil;
imageView = "(...not nil..)";
location = "(...not nil..)";
outfitArticleViewProperties = (
0x12b5010 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/OutfitArticleViewProperties/p1>
);
ownesOrWants = 0;
pattern = nil;
price = nil;
retailer = nil;
thumbnail = "(...not nil..)";
washRequirements = nil;
wearableSeasons = nil;
});
NSValidationErrorValue = <ArticleImage: 0x12ad600> (entity: ArticleImage; id: 0x12b4d00 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/ArticleImage/p1> ; data: {
article = 0x12b3f10 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/Article/p1>;
image = "(...not nil..)";
});
}
1600 错误是:
NSValidationRelationshipDeniedDeleteError
表示某些关系的错误代码
使用删除规则 NSDeleteRuleDeny 是
非空。适用于 Mac OS X v10.4 及更高版本。
在 CoreDataErrors.h 中声明。
但我一辈子都看不到哪种关系会阻止删除。如果某个 Core Data 向导能看出我的做法的错误,我会感到谦卑。
我无法将此标记为已解决,因为我并没有真正解决它,但我确实有一个可行的解决方法。在我的每个managedObjects 的.m 中,我添加了一个看起来像这样的方法:
-(void) deleteFromManangedObjectContext{
self.outfit = nil;
self.article = nil;
[[self managedObjectContext] deleteObject:self];
}
所以你可以看到,首先我手动清除了关系,然后我让对象自己删除。在其他对象中,不是nil-ing,而是在某些对象关系上调用我的删除方法,以获得级联。
【问题讨论】:
-
更新:我一直在做很多实验。如果我将所有删除规则都设置为“无操作”,那么我就可以删除了。当然,这不是我想要的行为,让一切都处于不一致的状态。如果我的任何关系船设置为 Cascade 或 Nullify,那么我将收到我上面提出的错误。
-
另一个更新:正如亚历克斯所指出的,此错误旨在传达您将删除规则设置为拒绝。我没有将任何删除规则设置为拒绝。我已经向苹果提交了一份关于错误报告此错误的错误报告。如果他们回应,我会告诉你的。这仍然不能解释我的模型出了什么问题,但正确的错误代码肯定会有所帮助!
-
另一个更新:我不会放弃这个。我对我的代码进行了一些修改,使我能够删除 OutfitArticleViewProperties。两种关系(服装和文章)的删除规则都是 Nullify。当我尝试删除时,我得到了与以前相同的错误。这提出了一个非常有趣的问题,如果删除规则为 Nullify,怎么会出错?我没有要求它在关系结束时删除对象,只是将关系的目标设置为 nil。
-
...哦,在你问之前,是的,这种关系是可选的。
-
感谢您的努力。我有同样的问题。我检查了我所有的删除规则、覆盖的方法等......问题仍然不时存在。我最终得到了相同的解决方案,但它更像是“变通”,根本不令人满意。
标签: core-data cascading-deletes