Response mapping to get object with dynamic path restkit

前端 未结 2 1506
闹比i
闹比i 2021-01-25 13:56

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          


        
相关标签:
2条回答
  • 2021-01-25 14:36

    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"
    
    0 讨论(0)
  • 2021-01-25 14:42

    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.

    0 讨论(0)
提交回复
热议问题