RestKit GET query parameters

后端 未结 2 623
遥遥无期
遥遥无期 2021-01-12 17:18

I\'ve been using RestKit 0.10.0 for a while now and up until this point, I only posted serialized objects to my server:

[[RKObjectManager sharedManager] post         


        
2条回答
  •  广开言路
    2021-01-12 18:19

    I solved adding a Category to RKObjectLoader, that is:

    for method

    -(void)getObject:(id)object usingBlock:(RKObjectLoaderBlock)block;
    

    I added into the Category a modified method:

    -(void)getObject:(id)object queryParameters:(NSDictionary*)queryParameters usingBlock:(void(^)(RKObjectLoader *))block;
    

    Here it is the listing fpr file "RKObjectManager+QueryParameters":

    //
    //  RKObjectManager+QueryParameters.h
    //  AlphaClient
    //
    //  Created by Antonio Rossi on 14/07/12.
    //
    
    #import 
    
    @interface RKObjectManager (QueryParameters)
    
    - (void)getObject:(id)object queryParameters:(NSDictionary*)queryParameters usingBlock:(void(^)(RKObjectLoader *))block;
    - (void)sendObject:(id)object queryParameters:(NSDictionary*)queryParameters method:(RKRequestMethod)method usingBlock:(void(^)(RKObjectLoader *))block;
    
    @end
    

    Here is the listing for file "RKObjectManager+QueryParameters.m":

    //
    //  RKObjectManager+QueryParameters.m
    //  AlphaClient
    //
    //  Created by Antonio Rossi on 14/07/12.
    //
    
    #import "RKObjectManager+QueryParameters.h"
    
    @implementation RKObjectManager (QueryParameters)
    
    - (void)getObject:(id)object queryParameters:(NSDictionary*)queryParameters usingBlock:(void(^)(RKObjectLoader *loader))block {
        [self sendObject:object queryParameters:queryParameters method:RKRequestMethodGET usingBlock:block];
    }
    
    - (void)sendObject:(id)object queryParameters:(NSDictionary*)queryParameters method:(RKRequestMethod)method usingBlock:(void(^)(RKObjectLoader *))block {
        NSString *resourcePath = [self.router resourcePathForObject:object method:method];
        [self sendObject:object toResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
            loader.method = method;
    
            // need to transform the original URL because when method is GET the additional paramentes don't get added
            RKURL *originalBaseURL = [RKURL URLWithBaseURL:[loader.URL baseURL]];
            NSString *resourcePath = [self.router resourcePathForObject:object method:RKRequestMethodGET];
            RKURL *authTokenURL = [originalBaseURL URLByAppendingResourcePath:resourcePath queryParameters:queryParameters];
            [loader setURL:authTokenURL];
    
            block(loader);
        }];
    }
    
    @end
    

    One more step is to add #import "RKObjectManager+QueryParameters.h" in your implementation file. In this new method it is assumed that the router property of RKObjectManager has been defined before making a call to it.

提交回复
热议问题