RestKit删除不起作用 - 可能的映射问题

时间:2014-03-31 23:09:29

标签: ios restkit

我正在使用RestKit POSTing和DELETE一个数据对象。

POST工作正常,但删除失败。这是工作的POST代码。

+(void)postItemMappingInitializer{

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Item class]];
[responseMapping addAttributeMappingsFromDictionary:@{
                                                      @"id":    @"Id",
                                                      @"parent_id": @"parentId"
                                                    }];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                        method:RKRequestMethodPOST
                                                                                   pathPattern:@"item"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
[requestMapping addAttributeMappingsFromDictionary:@{
                                                     @"createTimestamp": @"create_ts",
                                                     @"parentId":    @"parent_id",
                                                     @"user":         @"user_id"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:[Item class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodPOST];

RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
}


+(void)postItem:(Item*)item
       success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
       failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[[RKObjectManager sharedManager] postObject:item path:@"item" parameters:nil success:success failure:failure];
}

我为Delete添加了另一个失败的映射,因为当我进行删除调用时,itemId没有被正确填充。

DELETE 'http://<myserver>/api/v1/item/:itemId':
E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x10cb0d3d0 {NSLocalizedRecoverySuggestion={"error":"requirement failed: Item Id is invalid"}

这是我为删除添加的映射

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Item class]];
[responseMapping addAttributeMappingsFromDictionary:@{
                                                      @"id":    @"itemId",
                                                      }];
// Create a response descriptor and add it to the RKObjectManager object.
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:@"item/:itemId"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"item/:itemId" method:RKRequestMethodAny]];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

欣赏任何见解!

THX

2 个答案:

答案 0 :(得分:0)

您的请求描述符对象期望在200-209的区域中的HTTP状态代码通常表示请求已成功但您的返回代码400意味着问题是客户端(例如请求)无法通过服务器由于语法错误。

Restkit有一个特殊的日志记录系统,它通过以下代码行提供有关网络重新性和对象映射的更多信息:

    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

再次运行您的代码并查看其失败的位置或使用更详细的错误消息更新您的问题

答案 1 :(得分:0)

我发现了这个问题..发布在这里,万一它可以帮助其他人..

这是一个非常微妙的错误 - 我正在传递路径参数,因为路径没有正确生成。设置为零修复问题

[[RKObjectManager sharedManager] deleteObject:item path:nil parameters:nil success:success failure:failure];