使用 Magical Record 在 NSDictionary 中导入数组

Posted

技术标签:

【中文标题】使用 Magical Record 在 NSDictionary 中导入数组【英文标题】:Import array inside NSDictionary with Magical Record 【发布时间】:2014-08-13 11:27:00 【问题描述】:

我正在使用 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”的***对象。从图中可以看出。 二级对象如下: 还设置了relatedByAttribute 和关系。那么如何将数据导入多个条目(从***对象到低级对象)?

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于使用 Magical Record 在 NSDictionary 中导入数组的主要内容,如果未能解决你的问题,请参考以下文章

Magical Record、多种配置和 Restkit

ios Magical Record保存在UIApplicationWillTerminateNotification上

ios Magical Record 保存在 UIApplicationWillTerminateNotification

AFNetwork、Magical Record和块内保存

带有 Magical Record 的导入记录不会立即显示在 tableView 中

AFNetworking 的 Magical Record 导入问题