使用Magical Record在NSDictionary中导入数组

时间:2014-08-13 11:27:00

标签: ios objective-c magicalrecord

我正在使用Magical Record导入从Web服务返回的数据。以下是json

{
    "notes": null,
    "logged_on": "2014-08-08",
    "updated_at": "2014-08-08T15:33:25-04:00",
    "user_id": 876,
    "url": "https://august.roundtriptohealth.com/entries/5006",
    "is_logged": true,
    "id": 5006,
    "entry_recording_activities": [
        {
            "recording_activity_id": 1,
            "updated_at": "2014-08-08T16:39:19-04:00",
            "url": "https://august.roundtriptohealth.com/entry_recording_activities/5006",
            "recording_activity": {
                "type_of_prompt": "textbox",
                "updated_at": "2014-07-10T15:55:14-04:00",
                "options": [],
                "regex_validation": {
                    "message": "Up to three digits",
                    "name": "three_digits",
                    "regex": "^(\\d){1,3}$",
                    "display": "0 to 999"
                },
                "url": "https://august.roundtriptohealth.com/recording_activities/1",
                "name": "Exercise Minutes",
                "id": 1,
                "cap_value": 360,
                "summary": null,
                "created_at": "2013-11-01T11:50:36-04:00",
                "content": "**30+ minutes = 1 point**\n\nChoose a physical activity that elevates your heart, increases your breathing, and can be sustained for 30 minutes or more.\n\nWhen you and your Travel Companion log this activity the **same day**, you earn a bonus point and can visit a new attraction.",
                "cap_message": "You have exceeded the maximum number of minutes."
            },
            "entry_id": 5006,
            "id": 5006,
            "value": "37",
            "created_at": "2014-07-14T23:41:04-04:00"
        },
        {
            "recording_activity_id": 1,
            "updated_at": "2014-08-08T15:33:24-04:00",
            "url": "https://august.roundtriptohealth.com/entry_recording_activities/16131",
            "recording_activity": {
                "type_of_prompt": "textbox",
                "updated_at": "2014-07-10T15:55:14-04:00",
                "options": [],
                "regex_validation": {
                    "message": "Up to three digits",
                    "name": "three_digits",
                    "regex": "^(\\d){1,3}$",
                    "display": "0 to 999"
                },
                "url": "https://august.roundtriptohealth.com/recording_activities/1",
                "name": "Exercise Minutes",
                "id": 1,
                "cap_value": 360,
                "summary": null,
                "created_at": "2013-11-01T11:50:36-04:00",
                "content": "**30+ minutes = 1 point**\n\nChoose a physical activity that elevates your heart, increases your breathing, and can be sustained for 30 minutes or more.\n\nWhen you and your Travel Companion log this activity the **same day**, you earn a bonus point and can visit a new attraction.",
                "cap_message": "You have exceeded the maximum number of minutes."
            },
            "entry_id": 5006,
            "id": 16131,
            "value": "45",
            "created_at": "2014-08-08T15:33:24-04:00"
        },
        {
            "recording_activity_id": 37,
            "updated_at": "2014-08-08T15:33:24-04:00",
            "url": "https://august.roundtriptohealth.com/entry_recording_activities/16132",
            "recording_activity": {
                "type_of_prompt": "checkbox",
                "updated_at": "2014-07-30T13:42:27-04:00",
                "options": [],
                "regex_validation": null,
                "url": "https://august.roundtriptohealth.com/recording_activities/37",
                "name": "Eat 2 Different Colored Fruit Servings",
                "id": 37,
                "cap_value": null,
                "summary": "You’ll make a couple of colorful choices on this week’s Tour Bus.",
                "created_at": "2013-11-08T10:17:55-05:00",
                "content": "By spreading daily choices across the rainbow of colors, you&rsquo;ll get the best produce has to offer &mdash; vitamins, minerals, fiber, and phytochemicals &mdash; for better health and energy. Have at least 2 <a href=\"http://www.chow.com/assets/2011/05/FRUIT_VEG_SERVINGS.pdf\" target=\"_blank\">fruit servings (2 cups)</a>, each from a different color group: red, orange, yellow/white, green, and blue/violet.",
                "cap_message": null
            },
            "entry_id": 5006,
            "id": 16132,
            "value": null,
            "created_at": "2014-08-08T15:33:24-04:00"
        }
    ],
    "created_at": "2014-08-08T15:33:24-04:00"
}

我可以使用方法导入顶级对象:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [Entry importFromArray:responseObject inContext:localContext];
        }
    }];

但是,第二级(内部数组entry_recording_activities)不会导入。我在数据模型文件中声明了条目。名为“Entry”的顶级对象。你可以从图像中看到。 enter image description here
第二级对象如下:
enter image description here relatedByAttribute和关系也被设置。那么如何将数据导入到许多条目(从顶级到较低级别的对象)?

1 个答案:

答案 0 :(得分:1)

单击activities属性。在您的relatedByAttribute下,添加'mappedKeyName',并添加嵌套路径。在这种情况下,entry_recording_activities。

基本问题是你已经定义了如何自动连接数据,但是没有告诉导入库数据相对于导入的开始。