Map Url Parameters to Objects Using Restkit

Map url parameters to objects using RESTKit

You want to use the path pattern you specify in your response descriptor. Then you want to use routing (RKRoute) and metadata during your mapping. The metadata includes a routing section which gives access to the parameters extracted from the URL path.

Some info on metadata here (the docs are a little lacking).

In your mapping you want to use:

@metadata.routing.parameters.id

As the mapping source key path.


To make routing work you need to add the route to your object manager:

[manager.router.routeSet addRoute:...

And then you need to make the request in a way that means the route is used, like getObjectsAtPathForRouteNamed:object:parameters:success:failure:.

RestKit GET query parameters

  1. Why doesn't the second solution work?

params is used to create a HTTP body, which is not used in a GET/HEAD request.

  1. Why is the first solution working without having to set the loader.targetObject to nil, although I do not have any root key path
    in the JSON response?

I think targetObject is nil by default. You normally don't set it, the request will create it if needed. The only time I use it is when requesting objects without primary keys or other weird problems.

  1. What are the cases where I should use the getObject:usingBlock method? What is its intended purpose?

This is a convenience method so you don't have to remember all the correct syntax. Internally it just sends an object load request using GET.

EDIT:

Use this if you have an object you want to update.

  1. What should I use loader.params for? The object mapping tutorial from the wiki says this property can be used to encapsulate POST
    parameters, but I do not see the point since I can wrap the parameters
    in the serialized object that is being sent with the method
    postObject:usingBlock.

Whatever you put in params will be serialized to an HTTP body (or body stream). Again, postObject:usingBlock: is just a convenience method so you don't have to remember everything.

RestKit is open source. If you are not sure how it works you are free to follow the parameters internally. If you app and web service is well designed, you should be able to use the convenience methods. Sometimes you can not, and then you can use the raw forms like you have done.

EDIT:
Q Hrm, quoting your bullet points messed up the numbers...

RestKit map request to one url with different post params

'Probably' - it would be nice it you added a few examples to the question (requests and associated responses).

Sending the requests is fine, though you will need to explicitly specify the path / route name to use.

For the response, you will most likely need to use an RKDynamicMapping which will inspect the incoming data and return the appropriate mapping to use.

If you can't tell the 'type' from the response data (instead, you can only tell because of the URL that was requested) then you may need to try mapping into all types and rejecting invalid content using KVC validation, or you might want to change your data model (not enough information in the question to determine which...).

Restkit map the response to a parent Entity - when response does not have parent entity values

You should really use a route so that you can use a path pattern to insert the conversation id. Then you can use the mapping metadata to access the id value in the mapping and use it to connect to the appropriate object.



Related Topics



Leave a reply



Submit