1

I am trying to serialize a SyncRewardDataRequestModel in my response body. "an_id" serializes fine. However, the SyncRewardDataInputModel objects within the NSArray always serializes to a empty NSArray. I have confirmed that I am passing a correct value in my NSArray - Does anyone see what is incorrect with my mapping?

Classes:

@interface SyncRewardDataInputModel : NSObject
@property (nonatomic,copy) NSNumber *test_id;
@end

@interface SyncRewardDataRequestModel : NSObject
@property (nonatomic,strong) NSArray *syncRewardDataInputs;
@property (nonatomic,copy) NSNumber *an_id;
@end

The following is my response descriptor:

     //Populate mapping
 RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromDictionary:@{@"an_id": @"an_id"}];


RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping mappingForClass:[SyncRewardDataInputModel class]];
[syncRewardDataInputsMapping addAttributeMappingsFromDictionary:@{@"test_id":   @"test_id"}];

//Combine
RKRelationshipMapping *arrayRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"syncRewardDataInputs" toKeyPath:@"downloadCardResponseDTOs" withMapping:syncRewardDataInputsMapping];

[requestMapping addPropertyMapping:arrayRelation];

//Put it in a request
 RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[SyncRewardDataRequestModel class] rootKeyPath:nil method:RKRequestMethodAny];

return requestDescriptor;

1 Answer 1

4

This line:

RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping mappingForClass:[SyncRewardDataInputModel class]];

should be:

RKObjectMapping *syncRewardDataInputsMapping = [RKObjectMapping requestMapping];

because for a request you are always trying to map to an NSMutableDictionary so that JSON can be generated from it for transmission.

Often you will have a mapping used for the received data, linked to a response descriptor, and you can use inverseMapping on that to generate the mapping to be used for your request descriptor.

Sign up to request clarification or add additional context in comments.

2 Comments

So I can make all my Mappings response mapping and then call inverse mapping if I want to use them as a request mapping?
Not everything can be reversed, but generally yes (standard nested mappings work well)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.