iOS - 未解决的错误导致崩溃:悬挂对无效对象的引用

时间:2012-09-21 09:19:53

标签: ios core-data ios5

我在iOS上遇到以下崩溃:

    Unresolved error Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn’t be completed. (Cocoa error 1550.)" UserInfo=0x74a30d0 {NSValidationErrorObject=<Exercise: 0x9097d50> (entity: Exercise; id: 0x9097970 <x-coredata://AF762754-4CD9-4386-A453-049CC8710DBF/Exercise/p124> ; data: {
    groupId = 21;
    id = 102;
    intensity = "0x906a7a0 <x-coredata://AF762754-4CD9-4386-A453-049CC8710DBF/Intensity/p125>";
    lengthMeasurable = 1;
    name = Running;
    owner = "0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4>";
    picId = 2719;
}), NSAffectedObjectsErrorKey=(
    "<ExerciseEvent: 0x9090910> (entity: ExerciseEvent; id: 0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4> ; data: <fault>)"
), Dangling reference to an invalid object.=null, NSValidationErrorKey=owner, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1550.), NSValidationErrorValue=<ExerciseEvent: 0x9090910> (entity: ExerciseEvent; id: 0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4> ; data: <fault>)}, {
    "Dangling reference to an invalid object." = "<null>";
    NSAffectedObjectsErrorKey =     (
        "<ExerciseEvent: 0x9090910> (entity: ExerciseEvent; id: 0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4> ; data: <fault>)"
    );
    NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1550.)";
    NSValidationErrorKey = owner;
    NSValidationErrorObject = "<Exercise: 0x9097d50> (entity: Exercise; id: 0x9097970 <x-coredata://AF762754-4CD9-4386-A453-049CC8710DBF/Exercise/p124> ; data: {\n    groupId = 21;\n    id = 102;\n    intensity = \"0x906a7a0 <x-coredata://AF762754-4CD9-4386-A453-049CC8710DBF/Intensity/p125>\";\n    lengthMeasurable = 1;\n    name = Running;\n    owner = \"0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4>\";\n    picId = 2719;\n})";
    NSValidationErrorValue = "<ExerciseEvent: 0x9090910> (entity: ExerciseEvent; id: 0x9096540 <x-coredata:///ExerciseEvent/t6817E9A9-9A4C-4044-BF20-FC15380B7C2F4> ; data: <fault>)";
}

我在某个时刻遇到了这个崩溃,那就是我想在ExerciseEvent中保存我的模型CoreData。所以我在我的视图中创建了一个这样的新实体: self.ee = [NSEntityDescription insertNewObjectForEntityForName:@"ExerciseEvent" inManagedObjectContext:context]

然后我有2个按钮,“保存”和“丢弃”。如果我按“丢弃”,我必须删除该实体,因此我不会在上下文中获得可空的ExerciseEvent。我在视图中以这种方式删除它:

[context deleteObject:self.ee];
[context save:nil];

然后当我打开所有ExerciseEvents的列表时,我得到了如上所述的以下崩溃。

1 个答案:

答案 0 :(得分:2)

这种错误通常是因为当Excercise对象与ExerciseEvent没有必要的互惠关系时经常设置不正确的关系。您正在尝试删除对象ExerciseEvent。 并且Exercise对象与此ExerciseEvent有关系,并且不能与此有空关系,但是当您删除ExerciseEvent时,将出现错误。 该对象是“悬空”,因为对象图表示它应该处于一种关系中,但它只是在空间中悬挂而未连接到任何其他对象。


所以你需要做的是在对象ExerciseEvent和Exercise之间设置正确的关系删除规则。应该有级联规则。您可以在此处找到详细信息:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html