Inverse Mappings and defaultValueForMissingAttribute

瀏覽次數:374 次
跳到第一則未讀訊息

jamieforrest

未讀,
2012年7月17日 下午4:16:412012/7/17
收件者:res...@googlegroups.com

I am seeing a problem where the destination object of an inverseMapping (an NSDictionary) is not getting keyPaths set properly when the value at that keyPath is nil.

I have an inverse mapping for which I have set setDefaultValueForMissingAttributes = YES, and when I debug the applyAttributeMappings method of RKObjectMappingOperation, I see that when an attribute is nil the code tries to set a value of nil for the given keyPath. These are lines 401-406 of RKObjectMappingOperation:

// Optionally set the default value for missing values

            if ([self.objectMapping shouldSetDefaultValueForMissingAttributes]) {

                [self.destinationObject setValue:[self.objectMapping defaultValueForMissingAttribute:attributeMapping.destinationKeyPath]

                                      forKeyPath:attributeMapping.destinationKeyPath];

                RKLogTrace(@"Setting nil for missing attribute value at keyPath '%@'", attributeMapping.sourceKeyPath);

            }

The code is going into the conditional and calling setValue:forKeyPath on the destinationObject. However, if I inspect self.destinationObject after the line that sets the value for the key path, I see that the keyPath has not been added to the object. The ramification of this is that the serialized object I am sending to the web service is missing the attribute at this keyPath.

If I pause the debugger on the RKLogTrace... line above, and then print out some info, you can see the problem (I've included a couple of important lines before my call to po):

restkit.object_mapping:RKObjectMappingOperation.m:399 Did not find mappable attribute value keyPath 'summary'

2012-07-17 16:09:30.726 ConnexionMobile[12257:10a03] T restkit.object_mapping:RKObjectMappingOperation.m:405 Setting nil for missing attribute value at keyPath 'summary'

(lldb) po [attributeMapping destinationKeyPath]

(NSString *) $3 = 0x001a60e0 Summary

(lldb) po [self destinationObject]

(id) $2 = 0x0a061390 {

    CategoryID = 23;

    ID = 15;

    InspectionID = 5;

}

You can see that the "Summary" field has not been added to the destination object. Is this a bug? Or am I missing something here?

Thanks,

Jamie

jamieforrest

未讀,
2012年7月17日 下午5:27:522012/7/17
收件者:res...@googlegroups.com

Ok, I have some more info on what appears to be a bug. In RKObjectMapping.m, line 334, the code returns nil as the defaultValueForMissingAttribute:

- (id)defaultValueForMissingAttribute:(NSString*)attributeName {

    return nil;

}

Back in RKObjectMappingOperation.m, when it tries to set the destination object's key path to nil, it fails. This is because nil is an invalid value for key paths in collections, and in this case our destination object is an NSDictionary because we are serializing an object in an inverse mapping.

If I modify RKObjectMapper.m to return [NSNull null] as the defaultValueForMissingAttribute it works, and the keyPath gets added correctly.

According to Apple: "The NSNull class defines a singleton object you use to represent null values in situations where nil is prohibited as a value (typically in a collection object such as an array or a dictionary)."

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NumbersandValues/Articles/Null.html

I'd be happy to submit a pull request for this fix, but before doing so I wanted to double check that I am not doing something wrong.

Thanks for any help you can give,

Jamie

MountainMath

未讀,
2012年9月19日 凌晨2:12:122012/9/19
收件者:res...@googlegroups.com
Just stumbled across the same issue and found the same solution. I say it's a bug and your fix is the way to go.

回覆所有人
回覆作者
轉寄
0 則新訊息