Reskit For Empty Parent

时间:2014-09-23 08:56:07

标签: core-data restkit-0.20

我的回应是

<people>
  <person>
    ...
  </person>
  <person>
    ...
  </person>
</people>

我想用我的coredata对象联系人进行人物映射。问题是当没有人记录被提取时,响应具有空人标签,并且它给出了我找不到可映射对象的例外。

我的映射如下:

+ (RKEntityMapping *)contactMapping {

    //RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Contact class]];
    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Contact"
                                               inManagedObjectStore:[[HRCoreData sharedInstance] objectStore]];

    NSDictionary *mappingDictionary = @{@"id.text": @"contactId",
                                    @"title.text": @"title",
                                    @"first-name.text": @"firstName",
                                    @"last-name.text": @"lastName",
                                    @"company-name.text": @"companyName"
                                    };

    [mapping addAttributeMappingsFromDictionary:mappingDictionary];
    mapping.identificationAttributes = @[@"contactId"];
    return mapping;


}

响应描述符设置为:

RKResponseDescriptor *personResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider contactMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"people.person" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:personResponseDescriptors];

如何检查子项是否存在或者父项是否为空并进行相应的映射?

@ EDIT-我用这种方式尝试了动态映射:

+ (RKDynamicMapping *) dynPeopleMapping {
    // Basic setup
    RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];

    RKEntityMapping *conMapping = [MappingProvider contactMapping];
    [conMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"person" toKeyPath:@"Contact" withMapping:dynamicMapping]];

    // Configure the dynamic mapping via a block    
    [dynamicMapping setObjectMappingForRepresentationBlock:^RKEntityMapping *(id representation) {
         if([representation valueForKeyPath:@"people.person"]){
             // what can i do here to map person with contact when it exists?
             return conMapping;
        }else
            return nil;
     }];

     return dynamicMapping;
}

但它不是映射人与人联系是数据集合和联系人是单人的核心数据对象。我该如何映射它们?

1 个答案:

答案 0 :(得分:0)

解决了!

谢谢@Wain。 我只是创建了人nsobject类和人nsmanaged对象。动态映射如下:

+ (RKDynamicMapping *) dynPeopleMapping {
    // Basic setup
    RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
    RKObjectMapping *peopleMapping = [RKObjectMapping mappingForClass:[HRPeople class]];
    RKEntityMapping *personMapping = [MappingProvider contactMapping];

    [dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
        NSLog(@"people.person:%@", [[representation valueForKeyPath:@"people.person"] description]);
        if([representation valueForKeyPath:@"person"]){
            return personMapping;
        }else
            return peopleMapping;
    }];
    return dynamicMapping;
}

响应描述符为:

RKResponseDescriptor *personResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider dynPeopleMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"people" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:personResponseDescriptors];