RESTKit null json response

50 Views Asked by At

With RESTKit I am making a GET request to receive an object but sometimes the server returns a response that is literally "null" with no object if there is no record in the database. The request fails with a "Loaded an unprocessable response (200) with content type 'application/json'" because it cannot map the "null" response to an object but I want the request to succeed and just return a nil object. Here is how I am making the GET request:

NSDictionary *objMapping = @{
                             @"PROPERTY": @"property",
                            };

RKObjectMapping *myMapping = [RKObjectMapping mappingForClass:[MyClass class]];
[myMapping addAttributeMappingsFromDictionary:objMapping];


RKObjectManager* objectManager = [RKObjectManager sharedManager];

NSString *methodURL = [NSString stringWithFormat:@"%@/MyMethod", basePath];

NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:methodURL parameters:@{@"myParam": myParamValue}];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:myMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:statusCodes];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     MyClass *myObj = [mappingResult firstObject];


     if (success)
     {
         success(myObj);
     }
 } failure:^(RKObjectRequestOperation *operation, NSError *error) {
     if(failure)
     {
         failure(error);
     }
 }];

[[RKObjectManager sharedManager] enqueueObjectRequestOperation:objectRequestOperation];
0

There are 0 best solutions below