0

尝试使用 RestKit 0.20.2 映射响应时出现此映射错误。当对多个描述符使用同一个类时,就会出现这个问题。错误如下:

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

这是我的场景:

  • A getObjectfrom 从具有特定 id 的服务器/api/repository/1/Customers/:id获取 a 。Customer

  • CustomerPosted to将 /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");
}];

Success posting customer with id (null)写入日志并遇到映射错误。

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

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

4

1 回答 1

1

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

一个典型的例子是:

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

于 2013-09-18T13:17:04.680 回答