I'm using Restkit with object mapping but I seem to be getting an error saying Encountered errors during mapping: Expected an object mapping for class of type 'Rating', provider returned one for 'Error' - now I have set up mapping and serialisation for both the Rating and Error objects, so I don't understand why it's confused. Can anyone help?
This is the mapping returned from the classes
// Rating class
+ (RKObjectMapping *)getRestKitObjectMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Rating class]];
[mapping mapAttributes:@"id", @"mics", @"mdate", @"ipaddress", nil];
[mapping mapKeyPath:@"user_id" toAttribute:@"userID"];
[mapping mapKeyPath:@"fid" toAttribute:@"fid"];
return mapping;
}
// Error class
+ (RKObjectMapping *)getRestKitObjectMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Error class]];
[mapping mapKeyPath:@"code" toAttribute:@"code"];
[mapping mapKeyPath:@"message" toAttribute:@"message"];
return mapping;
}
This is the object routing
[[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic/:id"];
[[RKObjectManager sharedManager].router routeClass:[Rating class] toResourcePath:@"/mic" forMethod:RKRequestMethodPOST];
[[RKObjectManager sharedManager].mappingProvider setMapping:[Rating getRestKitObjectMapping] forKeyPath:@"ratings"];
[[RKObjectManager sharedManager].mappingProvider setMapping:[Error getRestKitObjectMapping] forKeyPath:@"error"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Rating getRestKitObjectMapping] inverseMapping] forClass:[Rating class]];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[[Error getRestKitObjectMapping] inverseMapping] forClass:[Error class]];
Rating *rating = [[Rating alloc] init];
[[RKObjectManager sharedManager] postObject:rating delegate:self];
These are the delegate methods, the didLoadObjects does get called
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSLog(@"Object loading failed with error: %@ ----- body: %@", error.localizedDescription, objectLoader.response.bodyAsString);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"Objects received: %@", objects);
NSLog(@"Respond body: %@", objectLoader.response.bodyAsString);
}
Here is the JSON response
{"error":{"error":1,"code":342,"message":"You have already rated this one"}}
Also when I look at the $_POST value on my REST PHP backend, it's actually empty which means that it's not receiving the posting object...why is that? My routing seems ok?