Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to process a rest response containing a nested dictionary using RestKit 0.20. here is the json response that I'm trying to process:

{
"name": "Tom",
"msgTo": "Hi ",
"msgFrom": "Hey",
"uuid": "4",
"ClientInfo": {
    "Tom": {
        "AttributeA": "0",
        "AttributeB": "28"
    },
    "Sam": {
        "AttributeA": "10",
        "AttributeB": "28"
    }
}

}

The issue is around the ClientInfo object. This object is a serialized java map of maps [ Map>] from the server using pojo jackson serialization.

here are the 2 models in iOS/RestKit I'm using to process the response:

@interface IAPClientInfo : NSObject
@property (weak, nonatomic) NSString * mapName;
@property (weak,nonatomic) NSString *propertyCount;
@property (weak,nonatomic) NSString *affinityValue;
@end

@interface IAPClientMessage : NSObject
@property (weak, nonatomic) NSString * name;
@property (weak, nonatomic) NSString * msgTo;
@property (weak, nonatomic) NSString * msgFrom;
@property (weak, nonatomic) NSString * uuid;
// should this be an array?
@property (weak, nonatomic) NSArray * iapClientInfoArray;
@end

here is the code

// inner mapping
RKObjectMapping *clientInfoMapping = [RKObjectMapping mappingForClass:[IAPClientInfo class]];
[clientInfoMapping setForceCollectionMapping:YES];
[clientInfoMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"mapName"];
[clientInfoMapping addAttributeMappingsFromDictionary:@{@"(mapName).AttributeA" : @"propertyCount", @"(mapName).AttributeB" : @"affinityValue"}];

// outer mapping
RKObjectMapping *messageMapping = [RKObjectMapping mappingForClass:[IAPClientMessage class]];    
[messageMapping addAttributeMappingsFromDictionary:@{@"msgFrom" : @"msgFrom",@"msgTo" : @"msgTo",@"uuid" : @"uuid",@"name" : @"name"}];

// relationship b/w inner and outer   
RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"ClientInfo" toKeyPath:@"iapClientInfoArray" withMapping:clientInfoMapping];
[messageMapping addPropertyMapping:relationshipMapping];

// response
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:messageMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:@""
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

When processing the response, the properties in the IAPClientMessage are all as expected except the iapClientInfoArray property, which is null. While there are mutiple IAPClientInfo objects, in the json response it's not clear what the property should be in the IAPClientMessage (currently set to NSArray*). Any suggestions to make this work properly?

tx.

share|improve this question
    
Are you able to get the server changed to return the ClientInfo as an array with the names as parameters in the dictionaries in the array? –  Wain Jun 11 '13 at 21:26
    
Try turning on trace logging RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); because your code looks ok. –  Wain Jun 11 '13 at 21:31

1 Answer 1

I've got this working by changing all the properties from 'weak' to 'strong'.

First, I activated the logging per your suggestion

RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

It showed that IAPClientInfo and IAPClientMessage objects were being populated correctly, suggesting there was no issue in the basic set-up.

The IAPClientInfo object was being populated correctly but was going null, hence I suspected there was a memory management issue.

Reading this article

http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

clarified the desired usage - I need all the properties to 'own' what they point to. Hence, I need them to use 'strong' not 'weak' semantics. I'm using ARC. The 'weak' semantics lead the pointer (I suspect) to point to 'nil' once the 'creating' code went out of scope.

Once the properties where changed in that way, everything worked as expected - the iapClientInfoArray * pointed to an array of properly constructed IAPClientInfo instances.

thanks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.