Mapping a RestKit Entity and Matching Parent Objects

I have a Person class (attributes firstName and lastName) and another Worker object (payroll and title attributes). In my Core Data model, the Person object is defined as the parent Worker object. In my AppDelegate, I'm setting up my mapping ...

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]; 

... in people.json ...

 { "workers": [ { "firstName":"Rob", "lastName":"Johnson" }, { "firstName":"John", "lastName":"Roberts" } ] } 

... now when I run this, the objects are not being seeded. How can I express the fact that the Worker class has the same mappings as Person? I could just add them to this comparison, but that seems wrong.

Also when registering Person mappings using ...

 [objectManager.mappingProvider addObjectMapping:personMapping]; 

... I do not use the RKManagedObjectMapping setMapping: forKeyPath: method because we will never experience only Person in this application, and therefore we will never display it. But I still want him to register. for child objects of Person.

+6
source share
1 answer

You do not tell the mapper object to do anything with the firstName and lastName fields. You just defined mappings for wage and title . In order for your seeding to work, you need to determine the mappings of the JSON structure as well as your web service and add something like:

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

Then, when you start sowing, your working cartographer will know how to match these values ​​with your people.json with your new Worker objects.

0
source

Source: https://habr.com/ru/post/916001/


All Articles