title: JSON to Core Data with Relationships
body: <p>Following <a href="http://www.raywenderlich.com/15916/how-to-synchronize-core-data-with-a-web-service-part-1" rel="nofollow noreferrer">Ray Wenderlich's new tutorial</a> I was able to get JSON data and store it into Core data. I am having a really hard time understanding how to do this with relationships in Core Data though. </p>
  
  <p>Here is my Data Model:</p>
  
  <p><img src="https://i.stack.imgur.com/RmY7a.png" alt="enter image description here"></p>
  
  <p>Here is my JSON:</p>
  
  <pre><code>{
      "results": [
          {
          "name": "Trivia 1",
          "objectId": "1000",
          "createdAt": "2012-08-31 18:02:52.249 +0000",
          "updatedAt": "2012-08-31 18:02:52.249 +0000",
          "questions": [
              {
                  "text": "Question 1"
              },
              {
                  "text": "Question 2"
              },
              {
                  "text": "Question 3"
              }
           ]
         }
       ]
  }
  </code></pre>
  
  <p>And finally here is where I set the managedObject's Value:</p>
  
  <pre><code>    //Sets values for ManagedObject, also checks type
      - (void)setValue:(id)value forKey:(NSString *)key forManagedObject:(NSManagedObject *)managedObject {
  
          NSLog(@"TYPE: %@", [value class]);
  
          //If managedObject key is "createdAt" or "updatedAt" format the date string to an nsdate
          if ([key isEqualToString:@"createdAt"] || [key isEqualToString:@"updatedAt"]) {
              NSDate *date = [self dateUsingStringFromAPI:value];
              //Set date object to managedObject
              [managedObject setValue:date forKey:key];
          } else if ([value isKindOfClass:[NSArray class]]) {  //&lt;---This would be the array for the Relationship
              //TODO: If it's a Dictionary/Array add logic here
              for(NSDictionary *dict in value){
                  NSLog(@"QUESTION");
              }
          } else {
              //Set managedObject's key to string
              [managedObject setValue:value forKey:key];
          }
      }
  </code></pre>
  
  <p>I have taken a look at <a href="https://stackoverflow.com/questions/2362323/json-and-core-data-on-the-iphone">this question</a> but I'm really confused how to connect the pieces together from the Ray Wenderlich examples. Any help would be greatly appreciated.</p>