尝试使用 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
getObject
from 从具有特定 id 的服务器/api/repository/1/Customers/:id
获取 a 。Customer
Customer
Posted 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'
我发现当省略另一个描述符时,这两个描述符各自独立工作。我也检查过,两条路径之间没有混淆。