我尝试从名为Timeline
的CoreData中添加一个包含Tree
和User
关系的已获取实体。
当我尝试从CoreData中获取它时,一切都很好,存在用户和树,并且没有设置为nil
。但是当我将它添加到我的数组时,用户关系会消失,但树不会消失,只有数组中的最后一个Timeline
实体保留了两者的引用。
这里是我在json上的循环,我通过id从CoreData获取Timeline
,或者如果CoreData在Timeline.get(withJSON:_, inManagedObjectContext:_)
内不知道他,则创建它:
var timelines = [Timeline]()
for jsonRow in json.arrayValue {
if let timeline = Timeline.get(withJSON: jsonRow, inManagedObjectContext: context) {
print("Timeline entitie from core data :\n", timeline)
timelines.append(timeline)
}
}
print("timelines array:\n", timelines)
我在内部循环打印的结果:
Timeline entitie from core data :
<myApp.Timeline: 0x6080002a1560> (entity: Timeline; id: 0xd0000000046c0006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p283> ; data: {
createdAt = "2016-12-03 17:25:21 +0000";
id = 21;
message = "blablabla";
tree = "0x60800023fa20 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A22>";
updatedAt = "2016-12-03 17:25:21 +0000";
url = nil;
user = "0xd000000000080000 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/User/p2>";
})
Timeline entitie from core data :
<myApp.Timeline: 0x6000000bc620> (entity: Timeline; id: 0xd0000000047c0006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p287> ; data: {
createdAt = "2016-12-04 12:17:23 +0000";
id = 22;
message = "jaw dawd awdkj alwkjd alwkjd lawkjd lakwjd lakwjdl kawjl dkajwldkja wlkdjawl kjdawlk jdawlkjaw lkjawlkawjlkw aj lkawjd lkawjd lkjawdl kjawd awdl kajwldk jawlkjd lakwj dlakwj ldkajw lkdjawlk jlakwjd la";
tree = "0x6000002354c0 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A23>";
updatedAt = "2016-12-04 12:17:23 +0000";
url = nil;
user = "0xd000000000080000 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/User/p2>";
})
Timeline entitie from core data :
<myApp.Timeline: 0x6080002a1b60> (entity: Timeline; id: 0xd000000004800006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p288> ; data: {
createdAt = "2016-12-04 15:19:02 +0000";
id = 30;
message = "Niche";
tree = "0x6080004279e0 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A31>";
updatedAt = "2016-12-04 15:19:02 +0000";
url = nil;
user = "0xd000000000080000 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/User/p2>";
})
这是我的数组的结果:
timelines array:
[<myApp.Timeline: 0x6080002a1560> (entity: Timeline; id: 0xd0000000046c0006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p283> ; data: {
createdAt = "2016-12-03 17:25:21 +0000";
id = 21;
message = "blablabla";
tree = "0x60800023fa20 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A22>";
updatedAt = "2016-12-03 17:25:21 +0000";
url = nil;
user = nil;
}), <myApp.Timeline: 0x6000000bc620> (entity: Timeline; id: 0xd0000000047c0006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p287> ; data: {
createdAt = "2016-12-04 12:17:23 +0000";
id = 22;
message = "jaw dawd awdkj alwkjd alwkjd lawkjd lakwjd lakwjdl kawjl dkajwldkja wlkdjawl kjdawlk jdawlkjaw lkjawlkawjlkw aj lkawjd lkawjd lkjawdl kjawd awdl kajwldk jawlkjd lakwj dlakwj ldkajw lkdjawlk jlakwjd la";
tree = "0x6000002354c0 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A23>";
updatedAt = "2016-12-04 12:17:23 +0000";
url = nil;
user = nil;
}), <myApp.Timeline: 0x6080002a1b60> (entity: Timeline; id: 0xd000000004800006 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/Timeline/p288> ; data: {
createdAt = "2016-12-04 15:19:02 +0000";
id = 30;
message = "Niche";
tree = "0x6080004279e0 <x-coredata:///Tree/t742495B8-E258-4C42-8E69-AC9776194B7A31>";
updatedAt = "2016-12-04 15:19:02 +0000";
url = nil;
user = "0xd000000000080000 <x-coredata://D5FDDD6B-2A13-4B03-B592-45C99D801439/User/p2>";
})]
谢谢你得到了答案,六行代码从未让我如此疯狂......
答案 0 :(得分:0)
看起来Timeline
和User
之间的关系是一对一的。在循环中,您为每个Timeline
提供相同的User
。当你这样做时,它们看起来很好。但由于这种关系是一对一的,因此每次将User
分配给新的Timeline
时,您也会忽略该关系的先前值。每个User
只能有一个Timeline
,因此当您将User
分配给新Timeline
时,旧版本会获得该关系的零值。这就是为什么它是最后一个具有非零值的原因,因为它是唯一一个在下一次循环中不会被改变的原因。
该怎么做取决于您的应用的工作方式。
User
应该只有一个Timeline
,请修复您的循环,以便每次分配不同的User
。User
可能有多个Timeline
,请将关系修改为User
到Timeline
之间的多对多。