RestKit POST response with wrong mapping

耗尽温柔 提交于 2019-12-08 08:30:23

问题


When I am doing a POST of an object, the response mapped to wrong object.

// 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];

The log:

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 is Article..., but it supposed to be Result...

I found the old similar issue here: https://github.com/RestKit/RestKit/issues/1081 I am using v0.21 (master branch), now I solve it by forcing 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];

Is that a RestKit issue or I am doing wrong?


回答1:


It is a RestKit design assumption. It does cause issues in some cases like yours. When posting and object, RestKit will always try to map the response into that same object.

Your workaround solution is a good alternative.

You could alternatively post a dictionary and have the result mapped back into that dictionary.



来源:https://stackoverflow.com/questions/19275071/restkit-post-response-with-wrong-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!