我有一个 Person 类(名字和姓氏的属性)和另一个实体 Worker(工资和头衔的属性)。在我的核心数据模型中,Person 实体被设置为 Worker 实体的父实体。在我的 AppDelegate 中,我设置了我的映射...
RKManagedObjectMapping* personMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person" inManagedObjectStore:objectManager.objectStore];
[personMapping mapKeyPath:@"firstName" toAttribute:@"firstName"];
[personMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[objectManager.mappingProvider addObjectMapping:personMapping];
RKManagedObjectMapping* workerMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Worker" inManagedObjectStore:objectManager.objectStore];
[workerMapping mapKeyPath:@"wage" toAttribute:@"wage"];
[workerMapping mapKeyPath:@"title" toAttribute:@"title"];
[objectManager.mappingProvider setMapping:workerMapping forKeyPath:@"workers"];
RKManagedObjectSeeder* seeder = [RKManagedObjectSeeder objectSeederWithObjectManager:objectManager];
[seeder seedObjectsFromFiles:@"people.json", nil];
...在people.json中...
{
"workers":
[
{
"firstName":"Rob",
"lastName":"Johnson"
},
{
"firstName":"John",
"lastName":"Roberts"
}
]
}
...现在当我运行它时,没有任何对象被播种。如何表达 Worker 类与 Person 具有相同映射的事实?我可以将它们添加到该映射中,但感觉不对。
此外,在使用...注册人员映射时
[objectManager.mappingProvider addObjectMapping:personMapping];
...我不使用 RKManagedObjectMapping 方法 setMapping:forKeyPath: 因为我们永远不会在这个应用程序中只体验一个 Person ,所以我们永远不会映射它。但我仍然希望它注册。对于 Person 的子实体。