带有错误映射的 RestKit POST 响应 [英] RestKit POST response with wrong mapping

查看:50
本文介绍了带有错误映射的 RestKit POST 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对对象进行 POST 时,响应映射到错误的对象.

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

日志:

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 是文章...,但它应该是结果...

targetObject is Article..., but it supposed to be Result...

我在这里发现了旧的类似问题:https://github.com/RestKit/RestKit/问题/1081我使用的是 v0.21(master 分支),现在我通过强制 targetObject 来解决它.

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

这是 RestKit 的问题还是我做错了?

Is that a RestKit issue or I am doing wrong?

推荐答案

这是一个 RestKit 设计假设.在像您这样的某些情况下,它确实会导致问题.在发布和对象时,RestKit 将始终尝试将响应映射到同一个对象中.

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.

这篇关于带有错误映射的 RestKit POST 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆