Search code examples
iosjsonrestkit

Response descriptor without root Json element


{
    "id": 76,
    "name": "Tom Timberley",
    "gender": 1,
    "follower_count": 1,
    "following_count": 1,
    "me_following": [{
        "id": 5,
        "status": "ACCEPTED",
        "following_me": {
            "id": 2,
            "status": "ACCEPTED"
        }
    }]
}

There is no outer Json object. Is there any way i can access the root object from object following_me when i do RKRelationshipMapping?

I have tables Following_me and User and I want to map the root of Json which is the user data to Following_me.

[following_me_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"@root" toKeyPath:@"user" withMapping:userMapping]];

Here following_me_mapping is the mapping for Following_me table and userMapping is the mapping for User table.

But specifying @root is not working.

[RKResponseDescriptor responseDescriptorWithMapping:mapping
                                             method:RKRequestMethodGET
                                        pathPattern:nil
                                            keyPath:nil
                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
 ];

This is how i add the response descriptor.


Solution

  • You seem to be thinking about the problem backwards and being confused about what your response descriptor is processing.

    By setting pathPattern:nil keyPath:nil your response descriptor is going to be active for any request you make (it isn't filtered by path pattern) and is going to process the JSON payload from the root (there is no key path to navigate to).

    So, your initial mapping is already supposed to deal with the root. Then you add a relationship for me_following key to process that content. In that mapping you add a relationship for following_me.

    In this way you drill down through the data mapping it as you go, rather than your description which says you want to drill in to begin with and then try to navigate back out again (which requires a rather different and more complex approach).