Search code examples
ios6ios7restkitrestkit-0.20

Create child object with RestKit


I'm new to RestKit but so far things seem to make sense, and I've setup some object mappings for a User object like so

+ (NSArray *) responseDecriptorsForEntityMapping:(RKEntityMapping *)entityMapping
{
    NSDictionary *mappings = @{
                               @"id": @"userID",
                               @"firstName": @"forename",
                               @"lastName": @"surname",
                               @"phoneNumber": @"phone",
                               @"emailId": @"email",
                               @"uri": @"uri",
                               };

    [entityMapping addAttributeMappingsFromDictionary:mappings];
    [entityMapping setIdentificationAttributes:@[@"userID"]];
    // Setup response descriptors
    RKResponseDescriptor *getDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping
                                                                                       method:RKRequestMethodGET
                                                                                  pathPattern:@"users/:userID"
                                                                                      keyPath:nil
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    return @[getDescriptor];
}

This is called from another class which sets the mappings for the object manager. This is working fine, and querying for a user object from the server works, so that's good.

What I want now is to create a child record. My User object contains a relationship to devices. What I want to now do, is create a new Device object and insert it into the devices set of the user.

Is there a way I can create a mapping that doesn't require me to specify the entire URL (i.e /users/:userID/devices) and have RestKit automatically calculate that?

If not, then that's ok, I can manually set that, but then second question, what is the general technique for creating the object, and putting it into the set? Would I just create the object as normal, and then once that's successfully created, simply use the normal core data method to add a new device, or can I configure RestKit to do this for me?


Solution

  • You usually use an RKRoute to specify what path pattern to use when making a particular type (GET / POST / ...) of request with a specified class of object. The route examines the object to extract the appropriate parameters to substitute into the path pattern.

    Once the route is configured, simply POST the object without supplying a path to the POST method. This causes the object manager to search for routes which match the type and class criteria so that it can build the required path.

    When POSTing, you can create the object yourself and RestKit will both update it with the received response and save the update into the context.