2012-05-17 206 views
6

我有一個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:因爲我們永遠不會在這個應用等體驗只是一個人我們永遠不會映射它。但我仍然希望它註冊。爲人的子實體。

+0

你可以試試這個:[RKObjectManager sharedManager] .mappingProvider setObjectMapping:personMapping forKeyPath:@ 「工人」]?我是restkit的新手,但據我瞭解,你必須以某種方式在你的keypath中包含「workers:」,並認爲這將做到這一點。爲什麼它被稱爲工作,但不返回工資和頭銜? – Pfitz

+0

謝謝,我會試一試!對於你的問題,我只是很快把它扔在一起,並沒有添加這些值。 – rob5408

+0

也許你可以試試這個:[personMapping mapKeyPath:@「workers.firstName」toAttribute:@「firstName」]; – Pfitz

回答

0

您不告訴對象映射器對firstNamelastName字段執行任何操作。您只定義了wagetitle的映射。爲了讓您的播種工作,你需要定義的JSON結構的映射,以及您的網絡服務,並添加類似:

[workerMapping mapKeyPath:@"workers.firstName" toAttribute:@"firstName"]; 
[workerMapping mapKeyPath:@"workers.lastName" toAttribute:@"lastName"]; 

然後,當你運行你播種,你的工人映射器就知道了如何將這些值從people.json映射到新的Worker對象。