title: RestKit 0.2 mapping Core Data <--> JSON with to-many relationships
body: <p>I've been battling with RestKit for over a week now and just can't seem to get my head around relationship mapping at all... I've tried reading the documentation and examples, but it just doesn't seem to make sense when I try and apply it in practise.</p>
  
  <p>I've temporarily abandoned my app and reduced everything down to a simple standalone project, which has the following data model:
  <img src="https://i.stack.imgur.com/c0iBP.png" alt="enter image description here"></p>
  
  <ul>
  <li>A Template owns a collection of Nodes.</li>
  <li>A subset of these nodes may also be referred to as 'links' within the template.</li>
  </ul>
  
  <p>The generated classes look like:</p>
  
  <pre><code>@interface Template : NSManagedObject
  @property (nonatomic, retain) NSString * guid;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSSet *links;
  @property (nonatomic, retain) NSSet *nodes;
  @end
  
  @interface Node : NSManagedObject
  @property (nonatomic, retain) NSString * desc;
  @property (nonatomic, retain) NSString * guid;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) Template *template;
  @end
  </code></pre>
  
  <p>I am trying to use RestKit mappings to map the core data model to JSON, and also the JSON back into core data. Eventually there will be a server component but I also want to use the same mappings to support import/export of the json to file.</p>
  
  <p>Here's an example of how I'd like the JSON to look for a Template:</p>
  
  <pre><code>{"nodes":[
      {"name":"Node 1","guid":"N-0001","desc":"This is the first node"},
      {"name":"Node 2","guid":"N-0002","desc":"This is the second node"},
      {"name":"Node 3","guid":"N-0003","desc":"This is the third node"}
  ],
  "guid":"T-0001",
  "name":"Template 1",
  "links":["N-0001","N-0002"]
  }
  </code></pre>
  
  <p>Here are the mappings that I'm currently using:</p>
  
  <pre><code>self.nodeMapping = [RKEntityMapping mappingForEntityForName:@"Node" inManagedObjectStore:self.rkManagedObjectStore];
  [self.nodeMapping addAttributeMappingsFromDictionary:@{
      @"guid": @"guid",
      @"name": @"name",
      @"desc": @"desc"
   }];
  [self.nodeMapping setIdentificationAttributes:@[@"guid"]];
  
  self.templateMapping = [RKEntityMapping mappingForEntityForName:@"Template" inManagedObjectStore:self.rkManagedObjectStore];
  [self.templateMapping addAttributeMappingsFromDictionary:@{
      @"guid": @"guid",
      @"name": @"name"
   }];
  [self.templateMapping setIdentificationAttributes:@[@"guid"]];
  
  [self.templateMapping addRelationshipMappingWithSourceKeyPath:@"nodes" mapping:self.nodeMapping];
  </code></pre>
  
  <p>This works for the 'nodes' relationship, but I can't for the life of me figure out how to map the links using IDs.</p>
  
  <p>I've tried using</p>
  
  <pre><code>[self.templateMapping addConnectionForRelationship:@"links" connectedBy:@"guid"];
  </code></pre>
  
  <p>but this just leaves the links property empty when I import the JSON above. </p>
  
  <p>I've also spent ages messing about with so many different forms of relationship mapping and attribute mapping and everything results in either cryptic errors from RestKit, or nothing happening at all.</p>
  
  <p>Can someone please tell me what I need to add to this mapping for the links to map correctly?</p>
  
  <p>I then plan to use the same mappings to dump a core data Template out to JSON. I'm hoping I can use [self.templateMapping inverseMapping] for this - but I've given up on this until I can  get the import mapping working. </p>
  
  <p>Thanks </p>