【问题标题】:Strange behavior of NSManagedObjects. Why they're nil?NSManagedObjects 的奇怪行为。为什么他们是零?
【发布时间】:2013-02-05 07:40:49
【问题描述】:

我在 CoreData 中添加新实体时遇到问题。在我的模型中是 Glyph -> 轮廓 -> 节点,每个节点都有原点、bcpIn 和 bcpOut - 所有 PointValue 类。我尝试添加具有新来源、bcpIn 和 bcpOut 的节点。但是在创建它们并分配回关系之后

origin.node = node;
bcpIn.node = node;
bcpOut.node = node;

只有 bcpIn 有正确的值。 Origin 和 bcpOut 的节点值为零 这是我的代码的一部分:

 [points willChangeValueForKey:@"selectedObjects"];

 TPGlyph * glyph;
 TPContour * contour;

 if (points.selectedObjects.count==1) {
                TPPointData * currentPoint = [points.selectedObjects objectAtIndex:0];
                contour = currentPoint.node.contour;
                glyph = currentPoint.node.contour.glyph;
                //index = contour



  } else if (points.selectedObjects.count==0){
                glyph = [glyphs.selectedObjects objectAtIndex:0];
                contour = [NSEntityDescription insertNewObjectForEntityForName:@"Contour" inManagedObjectContext:moc];
                contour.glyph = glyph;  
            }

  TPNode * node = [NSEntityDescription insertNewObjectForEntityForName:@"Node" inManagedObjectContext:moc];

  node.contour=contour;
  node.type = [NSNumber numberWithInt:NSLineToBezierPathElement];

  NSPoint point = [self rezoomPoint: firstClick ofGlyph:glyph];

  TPPointData * origin = [NSEntityDescription insertNewObjectForEntityForName:@"PointData" inManagedObjectContext:moc ];
  origin.pointValue = NSMakePoint(point.x, point.y);
  origin.node = node;
  node.origin = origin;

  TPPointData * bcpOut = [NSEntityDescription insertNewObjectForEntityForName:@"PointData" inManagedObjectContext:moc ];
  bcpOut.pointValue = NSMakePoint(point.x, point.y);
  bcpOut.node = node;
  node.bcpOut = bcpOut;

  TPPointData * bcpIn = [NSEntityDescription insertNewObjectForEntityForName:@"PointData" inManagedObjectContext:moc];
  bcpIn.pointValue = NSMakePoint(point.x, point.y);
  bcpIn.node = node;
  node.bcpIn = bcpIn;

  [points didChangeValueForKey:@"selectedObjects"];

  [contour addNodesObject:node];
  [points addObject:origin];
  NSLog(@"contour %@", contour);
  NSLog(@"node %@", node);
  NSLog(@"origin %@", origin);
  NSLog(@"bcpIn %@", bcpIn);
  NSLog(@"bcpOut%@", bcpOut);

输出是:

2013-02-01 09:50:04.410 AlwaysSmooth[59268:403] contour <TPContour: 0x1006c64e0> (entity: Contour; id: 0x105a07fc0 <x-coredata:///Contour/t338CE27E-8294-4455-AB79-BA5A70DD59AD2> ; data: {
    glyph = "0x100681350 <x-coredata://1CE61895-49D8-4BAB-BFE9-FC5762336779/Glyph/p2>";
    nodes =     (
        "0x100154e30 <x-coredata:///Node/t338CE27E-8294-4455-AB79-BA5A70DD59AD3>"
    );
})
2013-02-01 09:50:04.410 AlwaysSmooth[59268:403] node <TPNode: 0x1001aa0e0> (entity: Node; id: 0x100154e30 <x-coredata:///Node/t338CE27E-8294-4455-AB79-BA5A70DD59AD3> ; data: {
    bcpIn = "0x1001a8cf0 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD6>";
    bcpOut = "0x1001a9f60 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD5>";
    contour = "0x105a07fc0 <x-coredata:///Contour/t338CE27E-8294-4455-AB79-BA5A70DD59AD2>";
    next = "(...not nil..)";
    origin = "0x1001a8cf0 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD6>";
    prev = "(...not nil..)";
    smooth = 1;
    type = 1;
})
2013-02-01 09:50:04.411 AlwaysSmooth[59268:403] origin <TPPointData: 0x1001ee690> (entity: PointData; id: 0x1001c8d50 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD4> ; data: {
    node = nil;
    x = "-1936.5";
    y = "-981.8333";
})
2013-02-01 09:50:04.411 AlwaysSmooth[59268:403] bcpIn <TPPointData: 0x1001a8c90> (entity: PointData; id: 0x1001a8cf0 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD6> ; data: {
    node = "0x100154e30 <x-coredata:///Node/t338CE27E-8294-4455-AB79-BA5A70DD59AD3>";
    x = "-1936.5";
    y = "-981.8333";
})
2013-02-01 09:50:04.412 AlwaysSmooth[59268:403] bcpOut<TPPointData: 0x1001a9e80> (entity: PointData; id: 0x1001a9f60 <x-coredata:///PointData/t338CE27E-8294-4455-AB79-BA5A70DD59AD5> ; data: {
    node = nil;
    x = "-1936.5";
    y = "-981.8333";
})

origin 和 bcpOut 为 nil,但 bcpIn 具有正确的节点值。 有什么想法吗?

【问题讨论】:

  • 奇怪。如果您附上数据模型的屏幕截图(至少是 Node 和 PointData 实体),它可能会有所帮助。我从您的 NSLogs 中看到 node.bcpIn == node.origin,但我不知道为什么会从代码中发生这种情况。
  • 我认为这与自动发布有关。当我更改订单时,一切都开始工作了。

标签: objective-c xcode core-data


【解决方案1】:

自动发布问题?此命令有效:

node.origin = [NSEntityDescription insertNewObjectForEntityForName:@"PointData" inManagedObjectContext:moc ];
node.origin.pointValue = NSMakePoint(point.x, point.y);

【讨论】:

  • 从你所描述的关系来看,node.origin.node = node 完全,绝对是多余的。我不知道您的代码中有什么,但我认为您只是暂时掩盖了真正的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2021-06-01
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多