It’s the first time I use restkit and I am interested in the automatic mapping (and maybe later also in the integration with CoreData).
Currently I managed to perform a GET request and map the response to a simple object with the following code:
RKObjectMapping *myMapping = [[self class] objectMappingForClass:[MyClass class]];
[myMapping mapKeyPath:@"Name" toAttribute:@"name"];
[myMapping mapKeyPath:@"Value" toAttribute:@"value"];
....
+ (RKObjectMapping*)objectMappingForClass: (Class)class
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:class];
mapping.rootKeyPath = @"Data";
mapping.performKeyValueValidation = NO;
// mapping.ignoreUnknownKeyPaths = YES;
mapping.setNilForMissingRelationships = YES;
mapping.setDefaultValueForMissingAttributes = YES;
return mapping;
}
I retrieve the data from the server in this way:
RKObjectMapping *defaultPropertiesMapping = [self.objectManager.mappingProvider objectMappingForClass:[MyClass class]];
RKObjectLoader *request = [self.objectManager loaderWithResourcePath:GetDefaultPropertiesURL];
request.objectMapping = defaultPropertiesMapping;
RKResponse *response = [request sendSynchronously];
The problem is that for the response the server does not use the HTTP status, but an attribute in the JSON payload (the payload is something like:
{ "Status" : "OK", "Data" : { //real payload } }
How can I check that Status attribute??
If you set the object loader delegate to the class you are sending the request from, you can implement the following method:
You can then check the value of the status and handle the response accordingly. This method is called before the object is mapped to Core Data. Also, make sure your class adopts the RKObjectLoaderDelegate protocol or this won’t work.
If you ever want to remove data from the the response, use the following method:
Any keys removed from *mappableData won’t make it to the object mapper.