Question

Server team wants to separate out 2 PUT requests for one Item class like this -

PUT '<server>/item/:itemId'

and

PUT '<server>/item/:itemId/like'

I am using the following code in the initializer:

RKObjectManager *sharedRKObjectManager = [RKObjectManager sharedManager];
RKManagedObjectStore *managedObjectStore = [sharedRKObjectManager managedObjectStore];
// Create the mapping for the News entity.
RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:ENTITY_ITEM inManagedObjectStore:managedObjectStore];

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

And then the following is the call:

+(void)updateItem:(Item*)item
      withParams:(NSDictionary*)params
         success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
         failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
    [CMLRKSharedManager setUpHeaders];
    [[RKObjectManager sharedManager] putObject:item path:nil parameters:params success:success failure:failure];
}

My ques is - can i use the same functions for these 2 requests? Can i append the url any other way?

TIA!

Was it helpful?

Solution

Just don't use the RKRoute in this case. Explicitly supply the appropriate path when you call put....

Note that you should also have 2 response descriptors because the path patterns are different.

OTHER TIPS

in case it helps anyone.. here is the code to do this

NSString *path = [NSString stringWithFormat:@"item/%@/like", itemId];
[CMLRKSharedManager updateItem:item params:nil path:path success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Displaying user info %@", [mappingResult firstObject]);
} failure:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top