Response mapping to get object with dynamic path restkit

五迷三道 提交于 2019-12-04 05:30:54

问题


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 ?


回答1:


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"



回答2:


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.



来源:https://stackoverflow.com/questions/18180735/response-mapping-to-get-object-with-dynamic-path-restkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!