0
votes

I am having a problem with null JSON values being mapped to objects in Core Data, which causes errors. An example of a GET response from my server is:

{
    "status": "Success",
    "result": {
        "users": [
            {
                "id": 240,
                "username": "user",
                "fullName": "My Name",
                "profilePicture": {
                    "id": 12,
                    "ownerID": "240", 
                    "imageURL": "/photos/240/12",
                }
            },
            {
                "id": 241,
                "username": "username",
                "fullName": "Full Name",
                "profilePicture": NULL
            }
        ]
    }
}

I have set up RestKit 0.22.0 to map the JSON value for "profilePicture" as a relationship (of entity type Photo) for the User entity. The relationship profilePicture is optional for User entities. The problem occurs when the database returns a NULL value (AKA the user has not set a profile picture). RestKit will create a new Photo entity with all properties equal to nil and set it to the profilePicture relationship of the User entity. This causes many problems for me. How can I prevent NULL values from being mapped?

I have tried setting userMapping.assignsNilForMissingRelationships = YES, but this doesn't seem to work.

EDIT: I should point out that everything works great when the user has set their profile picture. I only get problems when the returned profilePicture value is NULL.

UPDATE: Even after changing the API to omit the "profilePicture" key/value pair altogether when NULL, RestKit still assigns the User entity's profilePicture relationship to a Photo entity with all nil attributes.

1
FWIW, these days if(dict[@"profilePicture"]) will give you the result you want there, right?Fattie
@JoeBlow yes, except the mapping is handled automatically by RestKit.Adam K
Fair enough. I'm kind of in the "RestKit is now redundant" camp, man!Fattie
I agree. If the deadline for this project wasn't so soon, I would completely remove RestKit and handle all the mapping myself with AFNetworking. RestKit seems to have become too "smart".Adam K

1 Answers

0
votes

Even after changing the API to omit the "profilePicture" key/value pair altogether when NULL, RestKit still assigns the User entity's profilePicture relationship to a Photo entity with all nil attributes.

That should be impossible. If the key isn't in the JSON the RestKit will not do any work or populate the relationship.

In any case KVC validation is what you want to use to validate any newly created photo entity and abort the processing if the object is invalid. See discardsInvalidObjectsOnInsert.