Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to map RestKit response?

I use RestKit to interact with a REST api. For some of the actions, like HTTP PUT/POST/DELETE, I only care about the response status code (200, or 500 etc), and don't care about the response data, even though the API does send back data.

For performance consideration, is there a way to configure RestKit to avoid mapping the response? It seems that if I don't set up a response descriptor I am getting the error "no response descriptors match the response loaded"

like image 248
yichen Avatar asked May 11 '13 18:05

yichen


3 Answers

My solution was just to use a mapping for an NSObject

RKObjectMapping * emptyMapping = [RKObjectMapping mappingForClass:[NSObject class]];
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:emptyMapping
                                                                                         method:RKRequestMethodPOST
                                                                                    pathPattern:API_SURVEY_UPLOAD keyPath:nil
                                                                                    statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
like image 168
deepwinter Avatar answered Jan 02 '23 20:01

deepwinter


If you don't need to map the response data to objects nor map objects to request params, you might be interested in using AFHTTPClient, which is what RestKit 0.20 uses anyway. You can access the AFHTTPClient object that RestKit itself uses, so you don't have to set up the base URL or authentication headers, etc. yourself all over again.

Here's a simple GET example:

[[[RKObjectManager sharedManager] HTTPClient] getPath:@"http://example.com"
                                           parameters:nil
                                              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                  // handle success
                                              }
                                              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                  // response code is in operation.response.statusCode
                                              }];
like image 20
nioq Avatar answered Jan 02 '23 19:01

nioq


No, as the error suggests you need some response descriptor defined. It doesn't need to be complex (it can map a single data item like a status flag into an NSDictionary).

Don't worry about performance until you have reason to (profiling shows a problem).

That said, the most efficient way for RestKit to operate (at runtime) is to not have multiple response descriptors to search through, so be as specific as you can with path patterns and keypaths.

like image 35
Wain Avatar answered Jan 02 '23 19:01

Wain