我在尝试使用 RestKit 0.20.2 映射响应时遇到此映射错误。当对多个描述符使用相同的类时会引入问题。错误如下:
RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type 'Customer', provider returned one for 'CustomerUploadResponse'
这是我的场景:
来自
/api/repository/1/Customers/:id
的getObject
从服务器获取具有特定属性的Customer
编号。发布到
/api/repository/1/Customers
的Customer
将其上传到服务器,并期望一个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'
我发现,当省略另一个描述符时,这两个描述符各自可以正常工作。 我也检查过,两条路径之间没有混淆。
最佳答案
为什么有两个独立的响应描述符? 一个就够了! 当您发布一个对象时,RestKit 期望返回相同的对象,因此会抛出映射错误。 从你的映射中,你有相同的对象,你不需要创建 2 个映射。
一个典型的例子是:
您有一个客户对象(没有 ID)并将其发布到您的网络服务。 Web 服务将其插入到数据库中并填写 id(最初缺少)。 Web 服务返回填写的对象,RestKit 自动将客户对象中缺少的 id 字段映射到返回的填写字段。
关于ios - Restkit - 预期对象映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873042/