RKRelationshipMapping与Parent Entity Restkit

时间:2013-09-05 06:02:05

标签: restkit

我有一个核心数据关系映射,如下所示:

enter image description here

这个JSON:

{
  user: {
    rid: '1234'
    providers: {
       [
         foursquare: {
           rid: '1234'
         },
         facebook: {
           rid: '1234'
         }
       ]
    }
  }

使用此映射:

    RKEntityMapping *foursquareProviderMapping = [RKEntityMapping mappingForEntityForName:@"FoursquareProvider" inManagedObjectStore:managedObjectStore];
foursquareProviderMapping.identificationAttributes = @[@"rid"];
[foursquareProviderMapping addAttributeMappingsFromArray:@[@"rid"]];

RKEntityMapping *facebookProviderMapping = [RKEntityMapping mappingForEntityForName:@"FacebookProvider" inManagedObjectStore:managedObjectStore];
facebookProviderMapping.identificationAttributes = @[@"rid"];
[facebookProviderMapping addAttributeMappingsFromArray:@[@"rid"]];

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[@"rid"];
[userMapping addAttributeMappingsFromArray:@[@"rid"]];
[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"providers.foursquare" toKeyPath:@"providers" withMapping:foursquareProviderMapping]];
[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"providers.facebook" toKeyPath:@"providers" withMapping:facebookProviderMapping]];

这会导致toKeyPath的多键路径映射错误:@“providers”

有没有正确的方法来映射?如果需要更改,我可以完全控制JSON。

1 个答案:

答案 0 :(得分:0)

您可以使用RKDynamicMapping来解决此问题。首先,你应该改变你的JSON。

{
user: {
rid: '1234'
providers: [
         {
         type: "foursquare"
         rid: '1234'
         },
         {
         type: "facebook"
         rid: '1234'
         }
     ]
  }
}

之后使用像这样的动态映射

RKEntityMapping *foursquareProviderMapping = [RKEntityMapping mappingForEntityForName:@"FoursquareProvider" inManagedObjectStore:managedObjectStore];
foursquareProviderMapping.identificationAttributes = @[@"rid"];
[foursquareProviderMapping addAttributeMappingsFromArray:@[@"rid"]];

RKEntityMapping *facebookProviderMapping = [RKEntityMapping mappingForEntityForName:@"FacebookProvider" inManagedObjectStore:managedObjectStore];
facebookProviderMapping.identificationAttributes = @[@"rid"];
[facebookProviderMapping addAttributeMappingsFromArray:@[@"rid"]];

RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"type" expectedValue:@"facebook" objectMapping:facebookProviderMapping]];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"type" expectedValue:@"foursquare" objectMapping:foursquareProviderMapping]];

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[@"rid"];
[userMapping addAttributeMappingsFromArray:@[@"rid"]];
[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"providers" toKeyPath:@"providers" withMapping:dynamicMapping]];