2013-06-04 73 views
0

我想發佈一個object1(託管對象)並接收另一個object2(不是託管對象)。 我收到以下錯誤: W restkit.object_mapping:RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type '<object1 class>', provider returned one for '<object2 class>'RestKit 0.20命名的發佈請求

我知道爲什麼會發生此錯誤,但不知道解決方案。

這裏是我的代碼,我是怎麼到目前爲止嘗試:
請求映射:

RKObjectMapping *objectMapping = [RKObjectMapping requestMapping]; 
[objectMapping addAttributeMappingsFromArray:@[@"prop1", @"prop2", @"prop3"]]; 
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:objectMapping objectClass:[<object1 class> class] rootKeyPath:nil]; 
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor]; 

響應映射:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[<object2 class> class]]; 
[objectMapping addAttributeMappingsFromArray:@[@"prop4", @"prop5"]]; 
responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping pathPattern:@"myPattern" keyPath:@"" statusCodes:statusCodes]; 
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor]; 

路由:

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[<object1 class> class] pathPattern:@"myPattern" method:RKRequestMethodPOST]]; 

召喚:

[[RKObjectManager sharedManager] postObject:object1 path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
    // Success 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    // Fail 
}]; 

回答

2

不能使用postObject:如果接收到的對象是不一樣的發送對象。相反,您需要自己生成請求操作(RKObjectRequestOperation),然後您可以指定傳出數據的映射並使用其他映射處理傳入數據。

查看使用RKObjectManager requestWithObject:生成請求,RKObjectRequestOperation initWithRequest:用於處理響應。

+0

好了,但在這種情況下,將RestKit做解析,而不是我? –

+0

是的,因爲您在創建響應操作時指定了響應描述符。 – Wain

+0

但基礎網址和其他網址相關的東西不能被使用? –

1

我可以確認@Wain答案是正確的(我想感謝他解決了一個讓我失去3天的問題)。如果你想使用一個不同的對象的請求和響應,你應該使用類似的東西,執行您的POST請求:

NSURL *url = [NSURL URLWithString:_baseURL]; 
    RKObjectManager *restManager = [RKObjectManager managerWithBaseURL:url]; 

    NSMutableURLRequest * request = [restManager requestWithObject:_objectToPost method:RKRequestMethodPOST path:_pathPattern parameters:_parameters]; 
       RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; 
       [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

        // process the response 
       } failure:^(RKObjectRequestOperation *operation, NSError *error) { 

        // process the error 
       }]; 
    [restManager enqueueObjectRequestOperation:operation];