当我对一个对象进行 POST 时,响应映射到错误的对象。
// Resquest for post new article
RKObjectMapping* articleRequestMapping = [RKObjectMapping requestMapping];
[articleRequestMapping addAttributeMappingsFromDictionary:@{
@"title" : @"title",
@"body" : @"body",
}];
RKRequestDescriptor *requestDescriptorArticle = [RKRequestDescriptor
requestDescriptorWithMapping:articleRequestMapping
objectClass:[Article class]
rootKeyPath:nil
method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptorArticle];
// Response for post new article
// response.body={
// "result": {
// "ok": 1
// }
// }
RKObjectMapping *resultMapping = [RKObjectMapping mappingForClass:[Result class]];
[resultMapping addAttributeMappingsFromDictionary:@{
@"ok" : @"ok"
}];
RKResponseDescriptor *resArticleCreate = [RKResponseDescriptor
responseDescriptorWithMapping:resultMapping
method:RKRequestMethodPOST
pathPattern:[NSString stringWithFormat:@"%@%@",apiVersion,@"/articles"]
keyPath:@"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:resArticleCreate];
日志:
2013-10-09 07:05:43.335 TabbedDemo[35156:4703] D restkit.object_mapping:RKMapperOperation.m:378 Executing mapping operation for representation: {
result = {
ok = 1;
};
}
and targetObject: <Article: 0x7d88670>
targetObject 是 Article...,但它应该是 Result...
我在这里找到了旧的类似问题:https ://github.com/RestKit/RestKit/issues/1081 我正在使用 v0.21(主分支),现在我通过强制 targetObject 来解决它。
// normal one, but with wrong mapping object.
// [[RKObjectManager sharedManager] postObject:article path:nil parameters:@{@"_csrf" : self._csrf}
// success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
// NSLog(@"result:%@",[mappingResult firstObject]);
// }
// failure:^(RKObjectRequestOperation *operation, NSError *error) {
// NSLog(@"Hit error: %@", error);
// }];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:article
method:RKRequestMethodPOST
path:nil
parameters:@{@"_csrf" : self._csrf}];
Result *r1 = [Result new];
operation.targetObject = r1;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"result:%@",[mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Hit error: %@", error);
}];
[operation start];
这是 RestKit 问题还是我做错了?