lenhhoxung
lenhhoxung

Reputation: 2756

Import array inside NSDictionary with Magical Record

I'm using Magical Record to import data returned from a webservice. Following is the 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"
}

I can import top level object with method:

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

However, the second level (inside array entry_recording_activities) doesn't import. I've declared entries in data model file. The top level object named "Entry". You can see from the image. enter image description here
The second level object is as follow:
enter image description here The relatedByAttribute and relationships are set also. So how can I import data to many entries (from top level to lower level object)?

Upvotes: 0

Views: 268

Answers (1)

casademora
casademora

Reputation: 69667

Click on the activities property. Under your relatedByAttribute, add 'mappedKeyName', and add the nested path. In this case, entry_recording_activities.

The basic problem is you've defined how to auto-connect the data, but have not told the import library where the data is relative to the start of the import.

Upvotes: 1

Related Questions