我有一個Person類(firstName和lastName屬性),並形成其他單位職工(工資和職稱的屬性)。在我的Core Data模型中,Person實體被設置爲Worker實體的父級。在我的AppDelegate設置我的映射......RestKit實體映射和父實體映射
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:因爲我們永遠不會在這個應用等體驗只是一個人我們永遠不會映射它。但我仍然希望它註冊。爲人的子實體。
你可以試試這個:[RKObjectManager sharedManager] .mappingProvider setObjectMapping:personMapping forKeyPath:@ 「工人」]?我是restkit的新手,但據我瞭解,你必須以某種方式在你的keypath中包含「workers:」,並認爲這將做到這一點。爲什麼它被稱爲工作,但不返回工資和頭銜? – Pfitz
謝謝,我會試一試!對於你的問題,我只是很快把它扔在一起,並沒有添加這些值。 – rob5408
也許你可以試試這個:[personMapping mapKeyPath:@「workers.firstName」toAttribute:@「firstName」]; – Pfitz