Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit Dynamically Map Relationship Name based on Value

I am using RestKit to parse JSON and map it into Core Data NSManagedObjects. Here is a sample JSON.

{
    "events": [
        {
            "description": "...",
            "subject_type": "photo",
            "subject": {
                "id": 1,
                "thumb_url": "...",
                "medium_url": "...",
                "large_url": "..."
            }
        },
        {
            "description": "...",
            "subject_type": "user",
            "subject": {
                "id": 1,
                "username": "...",
                "followers": "..."
            }
        }
    ]
}

Using RKObjectMappingProvider and RKManagedObjectMapping I am mapping the "events" array into separate Core Data Event objects. This works fine.

Now Event has two relationships on it User and Photo. Now I need to map the subject array to the proper Core Data object based on the value of "subject_type"and set that to the correct relationship on Event.

I tried using RKDynamicObjectMapping but I don't know how to specify that for a "dynamic relationship". I need some way to set the name of the destination relationship based on the value of subject_type.

Any thoughts?

like image 856
brynbodayle Avatar asked Oct 31 '12 22:10

brynbodayle


1 Answers

In RestKit, RKDynamicMapping also allows the use of a specified block to do more complex dynamic mapping operations like the one that you describe. The specific method that gives this capability is called setObjectMappingForRepresentationBlock

Here are some docs on that method : http://restkit.org/api/latest/Classes/RKDynamicMapping.html#//api/name/setObjectMappingForRepresentationBlock:

The RestKit unit tests illustrate some simple cases for this method.

like image 193
Adam Marks Avatar answered Oct 21 '22 06:10

Adam Marks