4

Given the following JSON:

{
   "someKey":"someValue",
   "otherKey":"otherValue",
   "features":[
      "feature1",
      "feature2",
      "feature3"
   ]
}

I am mapping this JSON into NSManagedObjects with RKMapperOperation and RKEntityMapping(in this example I would have 2 entity mappings: one for the top level object and another one for my Feature class).

The top level object mapping is trivial: two attribute mappings plus a relationship one(features) for the relation with Feature.

My question is,how to map the features JSON array into an array of Feature objects? The Feature class has just one property name where I want to store "feature1", "feature2", etc plus a reference to the parent object (the top level one). Something like this:

@interface Feature : NSManagedObject

//In the implementation file both properties are declared with @dynamic.
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) MyTopLevelObject *myTopLevelObject;

@end

Any idea?

e1985
  • 6,239
  • 1
  • 24
  • 39
  • It's called RestKit Relationships. Check out the wiki here: https://github.com/RestKit/RestKit/wiki/Object-Mapping#relationships. – Attila H Jun 19 '13 at 09:41

3 Answers3

7

You need to use a nil key path:

RKEntityMapping *featureMapping = [RKEntityMapping mappingForEntityForName:...];
[featureMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"name"]];
featureMapping.identificationAttributes = @[ @"name" ];

Then, on your top level object mapping, define the relationship:

[topMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featureMapping]];

In your feature (in the model), myTopLevelObject should be defined as a bi-directional relationship to the top level object.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I can't get this working. Is this line [featureMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"name"]]; The same as NSDictionary *featuresAttributeMappings = @{@"name":@"name"}; [featureMapping addAttributeMappingsFromDictionary: featuresAttributeMappings]; – Mazen Kasser Jan 29 '14 at 23:34
  • @MazenKasser, no, it isn't, the nil is required. – Wain Jan 30 '14 at 00:10
  • thanks for your quick response. Yes sorry the nil is required here. But what I meant is that [featureMapping addPropertyMapping...] is the same as [featureMapping addAttributeMappingsFromDictionary:...] – Mazen Kasser Jan 30 '14 at 03:09
  • @MazenKasser assuming you don't want a nil, yes – Wain Jan 30 '14 at 08:29
7

If you are using Restkit 0.20+ then all you need to do is set the property that is representing the string array of your entity to Transformable.

For example, in this case your Feature entity has 3 properties:

someKey - String
otherKey - String
features - Transformable

Restkit will automatically map 'features' as a string array.

So once mapped, to access one of the strings in the features array would be as simple as:

[Feature.features objectAtIndex:?]

I just tried it and it works perfect.

Besi
  • 22,579
  • 24
  • 131
  • 223
Jesse
  • 2,674
  • 6
  • 30
  • 47
  • this is a great solution. So you mean to create only one entity called Feature. What if I have the Features is an object with different key values. Create another entity for Features, but how that can be done? – Mazen Kasser Jan 29 '14 at 23:12
1

I don't think you can map an array of strings into a ManagedObject like that. However, since Feature only has one name property you could just store it as an array into your MyTopLevelObject. You can do that by adding a features property to MyTopLevelObject in your datamodel with the type Transformable. RestKit will automatically parse the features a NSArray with NSStrings. You can then get the features as following:

MyTopLevelObject *topLevelObject = ... // get the object from the persistent store
NSArray *features = (NSArray*)topLevelObject.features; // this will contain the features as NSString objects
lammert
  • 1,456
  • 14
  • 20