RestKit 0.20使用不同的映射映射请求和响应

时间:2012-12-17 19:37:00

标签: objective-c restkit

我将我的iOS应用程序连接到一个宁静的Web服务(WCF),其中请求与响应看起来不一样,我真的很难在restkit文档中找到它。

基本上我有两个相关的课程。 ClassA有一个ClassB集合。当我将一个ClassB实例发送到webservice时,标识另一个类(ClassA)的pk的属性被称为“ClassA”,就像被激活一样。

但是,当服务器成功添加了它发送回来的对象时,并不是同一属性被称为“ClassB1”(微软方式)。

我认为我想使用两种不同的映射,因为只使用“inverseMapping”并不是真的为我做的。有人有什么想法吗?

3 个答案:

答案 0 :(得分:3)

你认为两个映射就是答案是正确的。假设您使用的是最新版本的RestKit,可以使用以下内容。

另请注意,为了真正利用RestKit提供的功能,JSON必须符合密钥值编码。例如,在此示例中,JSON包含在“Response”键中。

我刚从正在处理的项目中删除了这个并更改了所有名称。这应该让你走上正确的道路。

RKObjectMapping *postObjectMapping = [RKObjectMapping requestMapping];
[postObjectMapping addAttributeMappingsFromDictionary:@{
 @"param1" : @"param1",
 @"param2" : @"param2"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postObjectMapping objectClass:[PostObjectModel class] rootKeyPath:nil];

RKObjectMapping *returnedObjectMapping = [RKObjectMapping mappingForClass:[ReturnedModel class]];
[returnedObjectMapping addAttributeMappingsFromDictionary:@{
 @"returnedParam1" : @"returnedParam1",
 @"returnedParam2": @"returnedParam2",
 @"returnedParam3": @"returnedParam3"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:returnedObjectMapping pathPattern:nil keyPath:@"Response" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"errors" statusCodes:statusCodes];


PostModel *objectToBePosted = [PostModel new];
[login setParam1:something];
[login setParam2:something];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://BASEURLHERE"]];
[objectManager addRequestDescriptor:requestDescriptor];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager addResponseDescriptor:errorDescriptor];

NSMutableURLRequest *request = [objectManager requestWithObject:objectToBePosted method:RKRequestMethodPOST path:@"/REST/OF/PATH/GOES/HERE" parameters:nil];

RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request
    success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"Success block: %@", mappingResult);
    } failure: ^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Failed with error: %@", [error localizedDescription]);
    }];

[objectManager enqueueObjectRequestOperation:operation];

}

答案 1 :(得分:2)

使用RKEntityMapping stick和mElling的答案但替换:

RKObjectMapping *returnedObjectMapping = [RKObjectMapping mappingForClass:[ReturnedModel class]];
[returnedObjectMapping addAttributeMappingsFromDictionary:@{
 @"returnedParam1" : @"returnedParam1",
 @"returnedParam2": @"returnedParam2",
 @"returnedParam3": @"returnedParam3"}];

使用:

RKEntityMapping *returnedObjectMapping = [RKEntityMapping mappingForEntityForName:@"ReturnedModel" inManagedObjectStore:objectManager.managedObjectStore];
[returnedObjectMapping addAttributeMappingsFromArray:@[@"returnedParam1", @"returnedParam2", @"returnedParam3"]];

注意:在returnObjectMapping之前,您还必须将设置“objectManager”变量的行移动到某处。

答案 2 :(得分:0)

问题是通过在mElling的答案中使用RKObjectMapping来解决的,因为docksteaderluke建议但只有在我将RestKit更新为0.20-pre6之后。