JSON到具有关系的核心数据 [英] JSON to Core Data with Relationships
问题描述
按照,但我真的很困惑如何连接从雷Wenderlich示例一起。任何帮助将非常感激。
在你的for循环中,你要做一些特殊的handeling,如果你处理与QuestionGroup,你会知道该对象上的数组是问题(假设它是唯一的数组),所以你可以为字典中的每个条目创建一个新的问题对象。这将打破同步引擎的通用性,但如果需要,您可以通过一些额外的步骤重新获得它。
else if([value isKindOfClass:[NSArray class]]){
if([[managedObject entity] name ] isEqualToString:@QuestionGroup){
NSSet * questions = [NSMutableSet set];
for(NSDictionary * question in value){
//创建你的问题对象/记录
NSManagedObject * questionManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@QuestioninManagedObjectContext:managedObjectContext];
//设置你的问题对象
questionManagedObject.text = [question valueForKey:@text];
//将所有创建的问题对象存储在集合中
[questions addObject:questionManagedObject];
}
//将问题集分配给QuestionGroup上的关系
[managedObject setValue:questions forKey:@questions];
}
}
Following Ray Wenderlich's new tutorial 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.
Here is my Data Model:
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 here is where I set the managedObject's 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'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];
}
}
I have taken a look at this question but I'm really confused how to connect the pieces together from the Ray Wenderlich examples. Any help would be greatly appreciated.
In your for loop you are going to do some special handeling, if you're dealing with a QuestionGroup you will know that an array on that object is questions (assuming it is the only array) so you can create a new Question object for each entry in the dictionary. This is going to break the genericness of the sync engine but you could go through some extra steps to regain it if desired.
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"];
}
}
这篇关于JSON到具有关系的核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!