Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change values of attributes when mapping to REST using Restkit?

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?

like image 284
Jelle Avatar asked Jun 10 '12 14:06

Jelle


1 Answers

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).

like image 194
jpalten Avatar answered Nov 20 '22 04:11

jpalten