2012-12-17 3 views
0

내 iOS 앱을 응답과 동일한 모양으로 보이고 restkit 설명서에서 찾지 못하는 편안한 웹 서비스 (WCF)에 iOS 앱을 연결합니다.RestKit 0.20 다른 매핑을 사용하여지도 요청 및 회람

기본적으로 나는 관련된 두 개의 클래스가 있습니다. ClassA에는 ClassB 컬렉션이 있습니다. ClassB의 인스턴스를 webservice로 보내면 다른 클래스 (ClassA)의 pk를 식별하는 속성을 "ClassA"라고합니다.

그러나 서버가 개체를 성공적으로 추가하면 개체를 다시 보내고 같은 특성이 "ClassB1"(Microsoft 방식)이 아닙니다.

"inverseMapping"을 사용하는 것이 실제로 나를 위해하지 않기 때문에 두 가지 다른 매핑을 사용하고 싶습니다. 누구든지 아이디어가 있습니까?

답변

0

mKorea의 답변에서 docksteaderluke가 제안한대로 RKObjectMapping을 사용하여 문제를 해결했지만 RestKit을 0.20-pre6 버전으로 업데이트 한 후에 만 ​​문제가 해결되었습니다.

+2

mElling의 대답이 작동하게 된 이유가 무엇입니까? 그렇다면 자신의 답이 아니라 대답을 받아 들여야합니까? – PaReeOhNos

3

두 매핑이 답이라고 생각하는 것이 맞습니다. RestKit의 최신 버전을 사용한다고 가정하면 다음과 같은 기능이 작동합니다.

RestKit이 제공해야하는 것을 실제로 활용하려면 JSON이 Key Value Coding을 준수해야합니다. 예를 들어,이 예제에서 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]; 

}

+0

위대한 답변, 기본적으로 내가 무엇을하고 싶은지 제외하고는 RKEntityMapping 대신 사용하고 싶습니다! 이 작업을 수행 할 수 있습니까? –

+0

불행히도 엔티티 매핑을 통해이 시점까지 아무 것도 할 필요가 없었습니다. 내가 작업하고있는 데이터는 매우 가벼우므로 로컬 복사본을 저장하는 것이 우선 순위가 아닙니다. 제가 제안 할 수있는 것은 RestKit github 홈페이지의 엔티티 예제와 http://restkit.org/api/0.20.0/index.html의 api 문서를 살펴 보는 것입니다. 다행스럽게도 다른 누군가가 엔티티 매핑을 사용하여 작업을 시작했기를 바랍니다. – mElling

2

는 mElling의 답 RKEntityMapping 스틱을 사용하지만 교체하려면 :

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"변수를 어딘가에 b로 설정 한 선 반환 된 ObjectMapping 전에.

관련 문제