1
votes

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!

1
Have you set the serialisation type to JSON? - Wain
Yes I have. I have numerous features that are already working, including JSON response mapping / request serialization. - JYC

1 Answers

0
votes

If you just have an array of TagEntity instances then you can get the array of strings with:

NSArray *tagStrings = [tags valueForKey:@"tagName"];

Once you have that you can pass the array to RestKit (wrapped into an NSDictionary based on your sample JSON) or use NSJSONSerialization directly and use the underlying AFNetworking provided API to send the JSON.