0
votes

I'm using RestKit 0.20 and detected a curious mapping behavior when using a canonical notation with dots (.). I would be glad, if you can explain a simple way of handling an object mapping from a flattened object which I implemented as an NSManagedObject on IOS to an object relation on server side.

The mapping is almost correct but the mapping of geopoint.lat and geopoint.lon as extra fields is wrong, from my perspective and raises an exception on the server (unrecognized property exception).

The RKObjectMapping:

[mapping addAttributeMappingsFromDictionary:@{
 @"geopoint.lat": @"latitude",
 @"geopoint.lon": @"longitude",
 @"countrycode": @"countryId",
 @"county": @"county",
 @"postcode": @"postalcode",
 @"city": @"city",
 @"street": @"street",
 @"housenumber": @"housenumber"
 }];

The effective Result (shown as log output)

2013-01-29 09:29:38.856 CPlusApp[4285:907] T restkit.object_mapping:RKMappingOperation.m:514 Mapped relationship object from keyPath 'startAddress' to 'startAddress'. Value: {
city = aCity;
geopoint =     {
    lat = "48.8901234";
    lon = "8.818448999999998976";
};
"geopoint.lat" = "48.8901234";
"geopoint.lon" = "8.818448999999998976";
postcode = 91000;
street = Rennbrunnen;
}
2013-01-29 09:29:38.861 CPlusApp[4285:907] D restkit.object_mapping:RKMappingOperation.m:818 Finished mapping operation successfully...
1

1 Answers

0
votes

I've been in a similar situation, and there are two solutions that I can see:

1) Create a new class for the geopoint with properties for lat and lon. Then add geopoint as a property in your object's class. Remove the two mappings (geopoint.lat & geopoint.lon) from the dictionary and add a relationship from keyPath 'geopoint' to 'geopoint'. From then on you can access the latitude and longitude via object.geopoint.lat and object.geopoint.lon.

2) Remove the mapping for requests via something along the lines of:

RKEntityMapping *addressRequestMapping = [mapping inverseMapping];

[addressRequestMapping removePropertyMapping:[addressRequestMapping propertyMappingsBySourceKeyPath][@"geopoint.lat"]];
[addressRequestMapping removePropertyMapping:[addressRequestMapping propertyMappingsBySourceKeyPath][@"geopoint.lon"]];

[manager addRequestDescriptorsFromArray:@[

 [RKRequestDescriptor addressRequestMapping objectClass:[Address class] rootKeyPath:nil]

]];

Option 1 will allow you to amend the coordinates and PUT the data to the server whereas option 2 will not.