删除对象时CoreData的奇怪行为

Posted

技术标签:

【中文标题】删除对象时CoreData的奇怪行为【英文标题】:Strange behavior of CoreData when deleting objects 【发布时间】:2013-02-03 11:38:10 【问题描述】:

我尝试构建简单的贝塞尔曲线编辑器。型号和代码如下。 当我试图删除 node.bcpInDatanode.bcpOutData 只删除 node.originData 时,女巫根本没有任何意义。

这是我的模型:

这里是删除:方法:

-(void) delete:(id)sender 

    [undoManager beginUndoGrouping];
    [points willChangeValueForKey:@"arrangedObjects"];

    NSMutableArray * nodesToDelete = [NSMutableArray array];
    for (TPPointData * pointData in points.selectedObjects) 


        NSLog(@"PointData %@", pointData);
        NSLog(@"PointData.node = %@", pointData.node);
        TPNode * node = pointData.node;

        if (pointData == pointData.node.originData) 
            //delete everything
            [nodesToDelete addObject:pointData.node];
            if (pointData.node.bcpInData!=nil)   
                NSLog(@"deleting bcpInData: %@", pointData.node.bcpInData);
                [moc deleteObject:pointData.node.bcpInData];
                pointData.node = nil;
            
            if (pointData.node.bcpOutData!=nil) 
                NSLog(@"deleting bcpOutData: %@", pointData.node.bcpOutData);
                [moc deleteObject:pointData.node.bcpOutData];
                pointData.node = nil;

            
            NSLog(@"deleting originData: %@", pointData.node.bcpOutData);
            [moc deleteObject:pointData];
            pointData.node = nil;

         else  //delete just object

            [moc deleteObject:pointData];
            pointData.node = nil;
        
        NSLog(@"node: %@", node);   
    


    NSMutableArray * contoursToDelete = [NSMutableArray array];
    for (TPNode * node in nodesToDelete) 

        if (node.contour.nodes.count == 1) 
            [contoursToDelete addObject:node.contour];
        
        [moc deleteObject:node];
    

    for (TPContour * contour in contoursToDelete) 
        [moc deleteObject:contour];
    
    [points didChangeValueForKey:@"arrangedObjects"];
    [undoManager endUndoGrouping];

尝试删除后的列表来源

2013-02-03 12:11:34.849 AlwaysSmooth[42786:403] PointData <TPPointData: 0x105c06400> (entity: PointData; id: 0x100133810 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p327> ; data: 
    node = "0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328>";
    x = "215.5";
    y = "550.1667";
)
2013-02-03 12:11:34.850 AlwaysSmooth[42786:403] PointData.node = <TPNode: 0x105c13250> (entity: Node; id: 0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328> ; data: 
    attribute = nil;
    bcpIn = "(...not nil..)";
    bcpInData = "0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326>";
    bcpOut = "(...not nil..)";
    bcpOutData = "0x100133640 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p329>";
    contour = "0x1001c8c20 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Contour/p282>";
    next = "(...not nil..)";
    originData = "0x100133810 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p327>";
    prev = "(...not nil..)";
    smooth = 1;
    type = 2;
)
2013-02-03 12:11:34.851 AlwaysSmooth[42786:403] deleting bcpInData: <TPPointData: 0x100183d70> (entity: PointData; id: 0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326> ; data: 
    node = "0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328>";
    x = "301.4023";
    y = "544.7839";
)
2013-02-03 12:11:34.853 AlwaysSmooth[42786:403] deleting originData: (null)
2013-02-03 12:11:34.854 AlwaysSmooth[42786:403] node: <TPNode: 0x105c13250> (entity: Node; id: 0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328> ; data: 
    attribute = nil;
    bcpIn = "(...not nil..)";
    bcpInData = "0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326>";
    bcpOut = "(...not nil..)";
    bcpOutData = "0x100133640 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p329>";
    contour = "0x1001c8c20 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Contour/p282>";
    next = "(...not nil..)";
    originData = nil;
    prev = "(...not nil..)";
    smooth = 1;
    type = 2;
)

为什么它甚至不尝试删除 bcpOutDataoriginData 在删除前为空。删除后 bcpInDatabcpOutData 仍然存在于节点对象中。 撤消仍然是可能的。

当我尝试仅删除 bcpInDatabcpOutData 代码时,代码只会再次删除 originData

2013-02-03 12:29:13.526 AlwaysSmooth[42786:403] PointData <TPPointData: 0x100183d70> (entity: PointData; id: 0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326> ; data: 
    node = "0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328>";
    x = "301.4023";
    y = "544.7839";
)
2013-02-03 12:29:13.527 AlwaysSmooth[42786:403] PointData.node = <TPNode: 0x105c13250> (entity: Node; id: 0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328> ; data: 
    attribute = nil;
    bcpIn = "(...not nil..)";
    bcpInData = "0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326>";
    bcpOut = "(...not nil..)";
    bcpOutData = "0x100133640 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p329>";
    contour = "0x1001c8c20 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Contour/p282>";
    next = "(...not nil..)";
    originData = "0x100133810 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p327>";
    prev = "(...not nil..)";
    smooth = 1;
    type = 2;
)
2013-02-03 12:29:13.529 AlwaysSmooth[42786:403] node: <TPNode: 0x105c13250> (entity: Node; id: 0x100133830 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Node/p328> ; data: 
    attribute = nil;
    bcpIn = "(...not nil..)";
    bcpInData = "0x1001337b0 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p326>";
    bcpOut = "(...not nil..)";
    bcpOutData = "0x100133640 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/PointData/p329>";
    contour = "0x1001c8c20 <x-coredata://8E705B64-CB39-4FBD-B296-936E70835DC8/Contour/p282>";
    next = "(...not nil..)";
    originData = nil;
    prev = "(...not nil..)";
    smooth = 1;
    type = 2;
)

有什么帮助吗?

【问题讨论】:

你的关系删除规则是怎么设置的? 我尝试了两个方向的 nullifycascadedennyno action .没有任何变化。 请将您的整个问题写在顶部,然后将其分解为多个部分,并在其下方添加代码。某人阅读整个问题以了解您遇到的问题然后寻找细节比一次只获得一个问题更容易。 更好?英语不是我的母语,用英语写作和思考总是有点痛苦;) 【参考方案1】:

在点控制器中选择了 BcpInData 和 BcpOutData。已经足够了 [points removeSelectedObjects:@[pointData]]; 删除前。

【讨论】:

以上是关于删除对象时CoreData的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章

删除coredata中的对象后根据通知重新加载tableview显示奇怪的结果

从新的子上下文中删除对象

将 CoreData 与对象一起使用?

CoreData 查询最初在子对象上返回零休息 - 但在退出所有数据后

谷歌地图 SDK - 核心数据

SwiftUI TextField (MacOS) 的奇怪行为