I am currently using RestKit version 0.20.3 in my iOS project to communicate with my backend web service.
In some cases, my web service returns an array of tags (django-taggit) in string format and I needed to map each tag string into a core data entity.
// example JSON from web service
"response" : { "tags": ["tag1", "tag2", "tag3"] }
// example Core Data entities
@interface TagEntity : NSManagedObject
@property (nonatomic, retain) NSString *tagName;
@end
From the below discussion, I found a way to map an array of tag strings into core data objects.
https://groups.google.com/forum/#!topic/restkit/54eZFQIjl7c
tagEntityMapping = [RKEntityMapping mappingForEntityForName:@"TagEntity" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[tagEntityMapping addPropertyMapping:[RKAttributeMapping mappingFromKeyPath:nil toKeyPath:@"tagName"]]
tagEntityMapping.identificationAttributes = @[@"tagName"];
[resultEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"tags" toKeyPath:@"tags"withMapping:tagEntityMapping]];
Now, I am looking for a way to post an array of tag strings from Core Data objects to web service.
In other words, given that I have an array of TagEntity Core Data objects, I wish to send an array of [TagEntity tagName]
To achieve this, I used [resultEntityMapping inverseMapping] as a request mapping, but as result, I get
"request" : { "tags": [{"tag1": {}}, {"tag2": {}}, {"tag3": {}}] }
while what I really wish to get is
"request" : { "tags": ["tag1", "tag2", "tag3"] }
I would appreciate any help. Thanks!