Search code examples
iosobjective-crestkit

Allow Restkit to send NULL value for object


I am trying to get RestKit to send an empty value for field in an object I am PATCHing to a server.

I have an object that I send up and I change the assigned_to_id in the request

{"request":{"title":"Asdad", "status":"open", "assigned_to_id": 4}}

request is an NSManagedObject and assigned_to_id is a NSNumber

However, when I want to unassign the request, I perform:

request.assigned_to_id = [NSNull NULL];

But then RestKit sends the request as:

{"request":{"title":"Asdad", "status":"open"}}

It doesn't include the assigned_to_id since it's value is NULL. Is there a way to prevent this? I'd like for it to send:

{"request":{"title":"Asdad", "status":"open", "assigned_to_id": null }}

=== UPDATE ===

After doing some preliminary research, I've seen that you can use setDefaultValueForMissingAttributes but it doesn't seem like that is helping, since it's still not sending correctly.

RKObjectMapping *updateMapping = [RKObjectMapping requestMapping];
updateMapping.setDefaultValueForMissingAttributes = YES;
updateMapping.setNilForMissingRelationships = YES;
[updateMapping addAttributeMappingsFromArray:@[@"title", @"assigned_to_id", @"status"]];

Solution

  • I don't think this is the best solution, but...

    The assigned_to_id default value is 0, so when if the request becomes unassigned, I send up the following PATCH request:

    {"request":{"title":"Asdad", "status":"open", "assigned_to_id": 0 }}
    

    and then I use the API logic to make the request unassigned.

    If anyone figures out how to send the RestKit request with a parameter that is NULL let me know!