オブジェクトの POST を実行しているときに、応答が間違ったオブジェクトにマップされました。
// Resquest for post new article
RKObjectMapping* articleRequestMapping = [RKObjectMapping requestMapping];
[articleRequestMapping addAttributeMappingsFromDictionary:@{
@"title" : @"title",
@"body" : @"body",
}];
RKRequestDescriptor *requestDescriptorArticle = [RKRequestDescriptor
requestDescriptorWithMapping:articleRequestMapping
objectClass:[Article class]
rootKeyPath:nil
method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptorArticle];
// Response for post new article
// response.body={
// "result": {
// "ok": 1
// }
// }
RKObjectMapping *resultMapping = [RKObjectMapping mappingForClass:[Result class]];
[resultMapping addAttributeMappingsFromDictionary:@{
@"ok" : @"ok"
}];
RKResponseDescriptor *resArticleCreate = [RKResponseDescriptor
responseDescriptorWithMapping:resultMapping
method:RKRequestMethodPOST
pathPattern:[NSString stringWithFormat:@"%@%@",apiVersion,@"/articles"]
keyPath:@"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:resArticleCreate];
ログ:
2013-10-09 07:05:43.335 TabbedDemo[35156:4703] D restkit.object_mapping:RKMapperOperation.m:378 Executing mapping operation for representation: {
result = {
ok = 1;
};
}
and targetObject: <Article: 0x7d88670>
targetObject は Article... ですが、Result のはずです...
ここで古い同様の問題を見つけました: https://github.com/RestKit/RestKit/issues/1081 v0.21 (マスター ブランチ) を使用していますが、targetObject を強制することで解決します。
// normal one, but with wrong mapping object.
// [[RKObjectManager sharedManager] postObject:article path:nil parameters:@{@"_csrf" : self._csrf}
// success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
// NSLog(@"result:%@",[mappingResult firstObject]);
// }
// failure:^(RKObjectRequestOperation *operation, NSError *error) {
// NSLog(@"Hit error: %@", error);
// }];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:article
method:RKRequestMethodPOST
path:nil
parameters:@{@"_csrf" : self._csrf}];
Result *r1 = [Result new];
operation.targetObject = r1;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"result:%@",[mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Hit error: %@", error);
}];
[operation start];
それはRestKitの問題ですか、それとも間違っていますか?