2012-12-05 5 views
2

나는 다음과 같은 JSON이 있습니다RestKit - 외래 키의 가성 배열

{ 
    "users": [ 
     {"id": "1", "name": "John Doe"}, 
     {"id": "2", "name": "Bill Nye"} 
    ], 
    "groups": [ 
     {"id": "1", "name": "Group1", "users": ["1", "2"]}, 
     {"id": "2", "name": "Group2", "users": ["1"]} 
    ] 
} 

... 그리고 사용자 및 그룹 개체와 코어 데이터 모델을. 그룹 객체는 사용자에게 대다수 관계 (NSSet)를가집니다.

나는이 가능하다는 것을 표시하는 것 다음 스레드를 찾았지만, 이러한 매핑이 수행되는 방법을 아무런 설명도 포함되지 않은 :

https://github.com/RestKit/RestKit/issues/284

가 어떻게 그러한이 매핑을 수행 할을 각 그룹의 "사용자"관계가 올바르게 연결되어 있습니까?

참고 : JSON 사용자 및 그룹을 각각의 핵심 데이터 개체에 올바르게 매핑하는 매핑이 설정되어 있습니다. 그러나 각 그룹의 "사용자"NSSet은 비어 있습니다.

답변

3

그래서 이것을 RestKit 0.20 (pre2)을 사용하여 계산했습니다. 그런 다음

{ 
    "users": [ 
     {"id": "1", "name": "John Doe"}, 
     {"id": "2", "name": "Bill Nye"} 
    ], 
    "groups": [ 
     {"id": "1", "name": "Group1", "users": [{"id" : "1"}, {"id" : "2"}]}, 
     {"id": "2", "name": "Group2", "users": [{"id" : "1"}]} 
    ] 
} 

, 다음 매핑 : 마지막으로

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore]; 
userMapping.identificationAttributes = @[@"id"]; 
[userMapping addAttributeMappingsFromArray:@[@"id", @"name"]]; 
RKEntityMapping *groupMapping = [RKEntityMapping mappingForEntityForName:@"Group" inManagedObjectStore:managedObjectStore]; 
groupMapping.identificationAttributes = @[@"id"]; 
[groupMapping addAttributeMappingsFromArray:@[@"id", @"name"]]; 
[groupMapping addRelationshipMappingWithSourceKeyPath:@"users" mapping:userMapping]; 

그리고, 다음 responseDescriptors

JSON은 (그룹의 사용자 배열에 속성 이름을주의) 다음과 같이 변경 될 필요가 :

RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:classMapping pathPattern:@"/api/allthejson" keyPath:@"users" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
RKResponseDescriptor *groupResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:classMapping pathPattern:@"/api/allthejson" keyPath:@"groups" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
[objectManager addResponseDescriptorsArray:@[userResponseDescriptor, groupResponseDescriptor]]; 

그런 다음 RKObjectManager의 getObjectsAtPat h : 매개 변수 : 성공 : 실패 방법 및 완료!

1

RestKit에는 특히 모델링 관계에 관한 많은 문제가 있습니다. 매핑을 디버깅하는 것은 어렵습니다.

다음은 RestKit없이 설명하는 것을 다루는 코드입니다.

NSArray *userArray; 
// an array populated with NSManagedObjects 
// correctly converted from JSON to the User entity 

NSArray *groups = [jsonObject objectForKey:@"groups"]; 

for (NSDictionary *d in groups) { 
    Group *g = [NSEntityDescription insertNewObjectForEntityForName:@"Group" 
        inManagedObjectContext:_managedObjectContext]; 
    g.id = @([d[@"id"] intValue]); 
    g.name = d[@"name"]; 
    NSArray *users = d[@"users"]; 
    for (NSString *s in users) { 
     User *u = [[userArray filteredArrayUsingPredicate: 
     [NSPredicate predicateWithFormat:@"id = %@", @([s intValue])]] 
      objectAtIndex:0]; 
     [g addUsersObject:u]; 
    } 
} 
// save 
+0

감사합니다 Mundi ...하지만 내가 RestKit과 할 수없는 것을 찾고 있습니까? 다 대일 관계를 수분 할 때 잘 작동하는 것 같습니다. 어디에서나 볼 수있는 것은 그것이 실제로 가능하다는 것을 나타내는 것 같지만 그것을하는 법은 결코 문서화되지 않습니다. – docksteaderluke

+0

그것이 제 3 자 래퍼의 일반적인 문제입니다. 문서화되지 않은 문제가 발생할 때까지는 기능을 추가하지 않고 삶을 편하게 만들어야합니다. 그래서 공식 SDK를 고수하고 싶습니다. 죄송합니다 RestKit을 도와 드릴 수 없습니다. – Mundi

+0

도움을 주신 Mundi에게 감사드립니다. 나는 RestKit을 사용하여 그것을 알아낼 수 있었고 결과에 만족합니다. – docksteaderluke

관련 문제