Restkit - 预期的对象映射错误

时间:2013-09-18 13:03:47

标签: ios objective-c restkit

尝试使用RestKit 0.20.2映射响应时出现此映射错误。当为多个描述符使用相同的类时引入了该问题。错误如下:

RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type   'Customer', provider returned one for 'CustomerUploadResponse'

这是我的情景:

  • 来自getObject的{​​{1}}从具有特定ID的服务器中提取/api/repository/1/Customers/:id

  • 发布到Customer的{​​{1}}会将其上传到服务器,并期望返回Customer个对象。

所以我设置了请求和响应描述符如下:

/api/repository/1/Customers

使用以下内容获取客户时:

CustomerPostResponse

NSString* getCustomerPath = @"/api/repository/1/Customers/4834"; NSString* postCustomerPath = @"/api/repository/1/Customers"; //Post customer mapping and descriptor RKObjectMapping *postCustomerMapping = [RKObjectMapping requestMapping]; [postCustomerMapping addAttributeMappingsFromArray: [NSArray arrayWithObjects:@"ID",@"LastModifiedTime",@"Name",@"ApiKey",@"CustomFieldValues",nil]]; RKRequestDescriptor *postCustomerRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postCustomerMapping objectClass:[Customer class] rootKeyPath:nil]; [[RKObjectManager sharedManager] addRequestDescriptor:postCustomerRequestDescriptor]; //Post customer response mapping and descriptor RKObjectMapping *postCustomerResponseMapping = [RKObjectMapping mappingForClass:[CustomerUploadResponse class]]; [postCustomerResponseMapping addAttributeMappingsFromArray:[NSArray arrayWithObjects:@"ID",nil]]; RKResponseDescriptor *postCustomerResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:postCustomerResponseMapping pathPattern:postCustomerPath keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]]; [[RKObjectManager sharedManager] addResponseDescriptor:postCustomerResponseDescriptor]; //Get customer response mapping and descriptor RKObjectMapping *getCustomerResponseMapping = [RKObjectMapping mappingForClass:[Customer class]]; [getCustomerResponseMapping addAttributeMappingsFromArray:[NSArray arrayWithObjects:@"ID",@"LastModifiedTime",@"Name",@"ApiKey",@"CustomFieldValues",nil]]; RKResponseDescriptor *getCustomerResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:getCustomerResponseMapping pathPattern:getCustomerPath keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]]; [[RKObjectManager sharedManager] addResponseDescriptor:getCustomerResponseDescriptor]; 被写入日志。

但是,在使用以下内容发布客户时:

[[RKObjectManager sharedManager] getObject:nil path:getCustomerPath parameters:nil     success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Success fetching customer");
} failure:^(RKObjectRequestOperation *operation, NSError *error){
    NSLog(@"Failure fetching customer");
}];

Success fetching customer被写入日志并遇到映射错误。

[[RKObjectManager sharedManager] postObject:customer path:postCustomerPath parameters:nil   success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Success posting customer");
} failure:^(RKObjectRequestOperation *operation, NSError *error){
    NSLog(@"Failure posting customer");
}];

我发现当省略另一个时,两个描述符各自独立工作。 我也检查过,两条路径之间没有混淆。

1 个答案:

答案 0 :(得分:1)

为什么你有两个独立的响应描述符? 一个就足够了! 当您发布一个对象时,RestKit需要返回相同的对象,因此会抛出映射错误。 从映射中,您拥有相同的对象,您不需要创建2个映射。

一个典型的例子是:

您有一个客户对象(没有ID),并将其发布到您的Web服务。 Web服务将其插入到数据库中并填写id(最初缺少)。 webservice返回填充的对象,RestKit会自动映射客户对象中缺少的id字段,并返回填充的字段。