0

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

基本上我有两个相关的类。ClassA 有一个 ClassB 的集合。当我将 ClassB 的实例发送到 web 服务时,标识另一个类 (ClassA) 的 pk 的属性称为“ClassA”,正如预期的那样。

但是,当服务器成功添加对象时,它会将其发回,而不是相同的属性称为“ClassB1”(微软方式)。

我在想我想使用两种不同的映射,因为仅仅使用“inverseMapping”并没有真正为我做这件事。有人有什么想法吗?

4

3 回答 3

3

您认为两个映射是答案是正确的。假设您使用的是最新版本的 RestKit,类似以下的内容将起作用。

还要记住,为了真正利用 RestKit 提供的功能,JSON 必须符合键值编码。例如,在此示例中,JSON 包装在“响应”键中。

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

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];

}

于 2012-12-17T20:52:00.130 回答
2

要将 RKEntityMapping 棒与 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"]];

注意:您还必须将设置“objectManager”变量的行移到返回的ObjectMapping 之前的某个位置。

于 2013-02-11T15:32:07.680 回答
0

正如docksteaderluke建议的那样,通过在mElling的答案中使用RKObjectMapping解决了这个问题,但只有在我将RestKit更新到0.20-pre6版本之后。

于 2013-02-12T09:33:37.567 回答