0
votes

I am trying to post an object1 (managed object) and receive another object2 (not managed object). I receive the following error: W restkit.object_mapping:RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type '<object1 class>', provider returned one for '<object2 class>'

I know why this error happens, but don't know the solution.

Here's my code, how I was trying so far:
Request mapping:

RKObjectMapping *objectMapping = [RKObjectMapping requestMapping];
[objectMapping addAttributeMappingsFromArray:@[@"prop1", @"prop2", @"prop3"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:objectMapping objectClass:[<object1 class> class] rootKeyPath:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];

Response mapping:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[<object2 class> class]];
[objectMapping addAttributeMappingsFromArray:@[@"prop4", @"prop5"]];
responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping pathPattern:@"myPattern" keyPath:@"" statusCodes:statusCodes];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

Routing:

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[<object1 class> class] pathPattern:@"myPattern" method:RKRequestMethodPOST]];

The call:

[[RKObjectManager sharedManager] postObject:object1 path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    // Success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    // Fail
}];
2

2 Answers

2
votes

You can't use postObject: if the received object isn't the same as the sent object. Instead, you'll need to generate the request operation (RKObjectRequestOperation) yourself and then you can specify the mapping for the outgoing data and handle the incoming data with a different mapping.

Look at using RKObjectManager requestWithObject: to generate the request and RKObjectRequestOperation initWithRequest: for handling the response.

1
votes

I can confirm that @Wain answer is correct (I would like to thank him for solving a problem which made me losing 3 days). If you want to use a different object for the request and the response,you should perform your POST request using something like that:

 NSURL *url = [NSURL URLWithString:_baseURL];
    RKObjectManager *restManager = [RKObjectManager managerWithBaseURL:url];   

    NSMutableURLRequest * request = [restManager requestWithObject:_objectToPost method:RKRequestMethodPOST path:_pathPattern parameters:_parameters];
                RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
                [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                    // process the response
                } failure:^(RKObjectRequestOperation *operation, NSError *error) {

                    // process the error
                }];
    [restManager enqueueObjectRequestOperation:operation];