I have a core data object and a REST web interface that use different value for the same attribute. The managed object has the attribute named "type" that is an integer (enum), the REST interface has a string value named "type" (strings like: "truck", "car", "moped").
I transform the value from string to integer when getting the object using the RKObjectLoaderDelegate method objectLoader:willMapData:
. I hope that's how I should do this.
My question: When putting/posting the object, how or where do I transform the value from integer (enum) back to string values? Is there a moment in the serialization process where I can manipulate values for a put/post action? Should I perpahps subclass the serializer or RKManagedObjectMapping, or is my delegate being called at some point that I can change the values of an attribute?
You can solve this by creating a category for the (NSManagedObject) class you're mapping, and adding special properties in this category that will be used by Restkit. So add a property typeString in the category that converts the enum (type) to a string and map the attribute "typeString" to json, instead of "type". It looks like this for receiving:
[mapping mapKeyPath:@"type" toAttribute:@"typeString"]
and like this for sending:
[serializationMapping mapKeyPath:@"typeString" toAttribute:@"type"]
This way you don't need to use objectLoader:willMapData:
at all, and the conversion is nicely embedded in the object (category).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With