【问题标题】:iOS - Unresolved error causes crash: Dangling reference to an invalid objectiOS - 未解决的错误导致崩溃:悬挂对无效对象的引用
【发布时间】:2012-09-21 09:19:52
【问题描述】:

我在 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 的列表时,如上所述,我遇到了以下崩溃。

【问题讨论】:

    标签: ios core-data ios5


    【解决方案1】:

    这个错误通常是因为当一个Excercise 对象没有与ExerciseEvent 建立必要的互惠关系时,关系设置不正确。您正在尝试删除对象 ExerciseEvent。 而Exercise对象和这个ExerciseEvent有关系,不能和这个有空关系,但是当你删除ExerciseEvent时,会出现错误。 该对象是“悬空的”,因为对象图表明它应该处于关系中,但它只是悬挂在与任何其他对象无关的空间中。


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

    【讨论】:

    • 谢谢伙计,就是这样!我对 CoreData 中的删除没有任何操作,我将其更改为 Cascade :)
    猜你喜欢
    • 2011-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多