JSON to master data with relationships

Following the new Ray Wenderlich tutorial, I was able to get JSON data and save it in Core data. However, it’s very difficult for me to figure out how to do this with relationships in Core Data.

Here is my data model:

enter image description here

Here is my JSON:

{ "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" } ] } ] } 

And finally, this is where I set the managedObject Value:

  //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]]) { //<---This would be the array for the Relationship //TODO: If it a Dictionary/Array add logic here for(NSDictionary *dict in value){ NSLog(@"QUESTION"); } } else { //Set managedObject key to string [managedObject setValue:value forKey:key]; } } 

I looked at this question , but I'm really confused about how to combine the fragments with Ray Wenderlich's examples. Any help would be greatly appreciated.

+4
source share
2 answers

In your for loop, you are going to perform special processing, if you are dealing with a group of questions, you will find out that the array on this object is a question (if it is the only array), so you can create a new question object for each entry in the dictionary. This will violate the versatility of the synchronization mechanism, but you can go through a few additional steps to restore it if you want.

 else if ([value isKindOfClass:[NSArray class]]) { if ([[managedObject entity] name] isEqualToString:@"QuestionGroup") { NSSet *questions = [NSMutableSet set]; for (NSDictionary *question in value) { // create your question object/record NSManagedObject *questionManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:managedObjectContext]; // setup your question object questionManagedObject.text = [question valueForKey:@"text"]; // store all the created question objects in a set [questions addObject:questionManagedObject]; } // assign the set of questions to the relationship on QuestionGroup [managedObject setValue:questions forKey:@"questions"]; } } 
+6
source

I have used restkit in the past to handle this. I felt it was pretty hard for what I am doing, but now that I am working on another project that should solve the same problem, I do not find anything that will work better. I guess how his time to glow with restayk again.

Take a look at http://restkit.org

0
source

All Articles