Satyam Raikar
Satyam Raikar

Reputation: 473

Response mapping to get object with dynamic path restkit

how to i set response mapping to manager with path pattern ..if the getobjects at path is different from path pattern that is used to map the response.

[manager addResponseDescriptorsFromArray:
@[[RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
 pathPattern:A
 keyPath:nil
 statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]];



[manager getObjectsAtPath:A/ID
 parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@" Category success");
[self.delegate didReceiveAssignedCategories];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Category failure");
}];

response mapping path ie:A must be set to dynamic path used to getobject ie:A/ID . ex:

Call 1)

A = /getAllCategories

A/ID = /getAllCategories/123

call 2)

A = /getAllCategories

A/ID = /getAllCategories/456

response mapping is same for 123, 456 only while getting the objects i am using different urls ie: with id's attached. how to do that ?

Upvotes: 1

Views: 1525

Answers (2)

Satyam Raikar
Satyam Raikar

Reputation: 473

1) First create response mapping like

[manager addResponseDescriptorsFromArray:
@[[RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
pathPattern:@"getAllCategories/:categoryID"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]];

2)Create Class with categoryID in it.

[CategoryRequest class]

3) create object of that class and set categoryID

CategoryRequest *categoryRequest = [CategoryRequest alloc] init];
categoryRequest.categoryID = @"123";

4)call getobject using that object

[manager getObject:categoryRequest 
path:@"getAllCategories/123" 
parameters:params 
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                 NSLog(@"Success");
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

if another call is required to be made for same mapping create object of of category request class set new category id and call get object using that categoryresquest and required path patter.

Upvotes: 0

Wain
Wain

Reputation: 119031

If you have 2 path patters which both return the same type of data then you can use the same mapping with 2 different response descriptors.

If you have 1 path pattern which can return 2 different types of data then you need to use RKDynamicMapping to 'intercept' the incoming data and decide which mapping is actually required.


From your edited question, 'pattern' is the important thing that you have misunderstood. You need to use a path pattern, not a static path:

@"getAllCategories/:identity"

Upvotes: 3

Related Questions