HawkEye1194 HawkEye1194 - 5 months ago 25
Objective-C Question

Restkit Unable to add mapping for keyPath

Several of my objects have the field "id" and RestKit won't let me map both of them to the same name. How can I fix this? For these two objects I have a corresponding server side object with the MyId property that needs to get filled in. I don't want to have to rename the properties as a solution.

Here is an example of two classes I may use:

@interface MyObject : NSObject
@property NSNumber *id;
@end

@interface AnotherObject : NSObject
@property NSNumber *id;
@end


Here is an example of the code that I would run on both of these objects:

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
NSDictionary *objMapping = @{@"id": @"MyId"};

[userMapping addAttributeMappingsFromDictionary:objMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[MyObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];

objMapping = @{@"id": @"MyId"};

[userMapping addAttributeMappingsFromDictionary:objMapping];
requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[AnotherObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];


Here is the error that I would get:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath MyId, one already exists...'

Answer

The problem is simply that you're calling

[userMapping addAttributeMappingsFromDictionary:objMapping];

Multiple times with the same parameters and that isn't permitted

Comments