Search code examples
iosrestkit

RestKit Delete not working - Possible Mapping issue


I am POSTing and DELETEing a data object using RestKit.

POST works fine but delete is failing. Here is the working POST code.

+(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];
}

I added another mapping for Delete which is failing because when I make the delete call the itemId is not getting filled in correctly.

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"}

This is the mapping i added for delete

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

Appreciate any insights!

thx


Solution

  • I found the issue.. Posting here in case it helps someone else..

    It was a very subtle mistake - I was passing in the path param because of which the path wasnt getting generated correctly. Setting that to nil fixed the issue

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