iphone - JSON 到具有关系的核心数据

标签 iphone ios core-data one-to-many relationships

正在关注 Ray Wenderlich's new tutorial我能够获取 JSON 数据并将其存储到核心数据中。不过,我真的很难理解如何使用核心数据中的关系来做到这一点。

这是我的数据模型:

enter image description here

这是我的 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"
            }
         ]
       }
     ]
}

最后这里是我设置 managedObject 的值的地方:

    //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];
        }
    }

我看过this question但我真的很困惑如何将 Ray Wenderlich 示例中的各个部分连接在一起。任何帮助将不胜感激。

最佳答案

在你的 for 循环中你将做一些特殊的处理,如果你正在处理一个 QuestionGroup 你会知道该对象上的一个数组是问题(假设它是唯一的数组)所以你可以创建一个新的问题字典中每个条目的对象。这将打破同步引擎的通用性,但如果需要,您可以通过一些额外的步骤来重新获得它。

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

关于iphone - JSON 到具有关系的核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223214/

相关文章:

swift - 如何对包含 Core Data 过滤数据的 UITableView 实现滑动删除?

ios - 在我错过了 flutter 应用程序之后,有什么办法可以回到我的旧 Xcode

iphone - 自定义 UIView 在 [super dealloc] 上抛出 EXC_BAD_ACCESS

iOS 可用蓝牙设备列表,以编程方式提供信息

ios - 通过代码更改搜索占位符

ios - 如何在 iOS 中使用 oauth 在 Twitter 帐户中发布图像?

ios - UITableviewCell layoutSubviews 滑动删除时调用了几次

iphone - 如何只获取域名?

ios - Apple 在核心数据的上下文中将什么称为对象图?

core-data - 永久 NSManagedObjectID 不是那么永久吗?