Search code examples
iosjsonrestkitobject-object-mapping

How to use RKObjectMapping as a standalone library


I use AFNetworking to write a RESTful client. An then use JSONKit to parse the response data to a NSDictionary.

Example:

- (void)postPath:(NSString *)path
  parameters:(NSDictionary *)parameters
     success:(void (^)(AFHTTPRequestOperation *operation, id responseObject, NSDictionary* jsonDictionary))success
     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
    {

    [self.client postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* jsonDictionary = [self.jsonDecoder objectWithData:responseObject];

        // do the object-mapping works            

        success(operation, responseObject, jsonDictionary);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(failure, error);
    }];

}

How can I use the benefit of RKObjectMapping from RESTKit as a standalone library to do the object-mapping? Thanks in advance.


Solution

  • If you use RestKit it will replace all of your current code. RestKit is built on top of AFNetworking so you would post your object from RestKit and map the resulting data back into that object. Whether this is useful depends on the purpose of your POST. What you have currently can be trimmed a little if you don't use RestKit by using AFJSONRequestOperation.

    In general, the question boils down to:

    1. What is the data you're posting and expect to receive
    2. How complicated is your mapping

    If the mapping isn't complex, RestKit is probably overkill. From your code it looks like you're making a simple post so that should be fine (and you would construct your mappings to update the object you're posting, even though no data was actually taken from that object to make the request).