0

I'm having trouble mapping a particular JSON structure, as illustrated in a simplified form here:

{"personDetails":{"eyeColor":"brown",
                  "height":"2m 12cm",
                  "specialRestrictions":null,
                  "person":{"personId":42,
                            "firstName":"Hummingbird",
                            "lastName":"Collins",
                            "dob":1360856245000,
                            "gender":"F",
                            "personCode":"8DECCC6D-68CA-47E1-AV7F-84C2039D517",
                            "isAdmin":false}
                  }
}

In this case, I'd like to use the "personId" field, or even the "person" object itself, as a primary key of my "personDetails" object. As far as I can tell, there is no way to do this. I looked into RKConnectionDescription, but it doesn't seem applicable in this case, since the entire object is embedded, not just a foreign key attribute.

I basically want the relationship to be one-to-one, in that, when I call my service, like this,

http://server/services/getPersonDetailsByID/42

the details can map and persist in CoreData, overwriting the PersonDetails for that Person that were previously saved. At the moment, multiple PersonDetails objects can exist for the same Person locally, because there is no key in place.

So, my question is this: can RestKit mapping be set up to accomplish this intended behavior? Or will I need to handle the deletion of any outdated CoreData objects myself?

Edit: Here's how my mapping looks currently.

RKEntityMapping* personDetailsMapping = [RKEntityMapping mappingForEntityForName:
          @"personDetails" inManagedObjectStore:objectManager.managedObjectStore];
[personDetailsMapping addAttributeMappingsFromArray:@[@"eyeColor", @"height",  
                                                         @"specialRestrictions"]];
[personDetailsMapping addPropertyMapping:[RKRelationshipMapping
          relationshipMappingFromKeyPath:@"person" toKeyPath:@"person" 
                     withMapping:personMapping]]; // personMapping defined earlier

Here are two different ways I've tried adding an Identification attribute:

personDetailsMapping.identificationAttributes = @[@"person"];

and

personDetailsMapping.identificationAttributes = @[@"personId"];

at different times, and they each throw an error like this:

Invalid attribute 'personId': no attribute was found for the given name in the 'PersonDetails' entity.
4

1 回答 1

1

将密钥路径添加为标识属性可能会起作用。我实际上没有尝试过,但关键路径在大多数地方都有效。

personDetailsMapping.identificationAttributes = @[ @"person.personId" ]

如果没有,您可以设置映射以将人员 ID 复制到详细信息对象中。您将需要添加一个新的持久属性,然后在映射中使用键路径:

@"personId" : @"person.personId"
于 2013-08-07T14:20:15.333 回答