2013-03-02 46 views
1

我正在嘗試使用RestKit將一些JSON映射到我的核心數據實體。如何使用RestKit指定關係?

我有一個Case實體與caseId屬性和一個可選的一個一對多的關係,稱爲binders一個Binder實體。 Binder實體與Case具有反向一對一關係,稱爲case,這不是可選的。

我可以成功映射Case實體,但我在映射Binder實體時遇到問題。

我的映射設置如下:

RKEntityMapping *caseEntityMapping = [RKEntityMapping mappingForEntityForName:@"Case" inManagedObjectStore:managedObjectStore]; 
[caseEntityMapping addAttributeMappingsFromArray:[@"caseId"]]; 

RKEntityMapping *binderEntityMapping = [RKEntityMapping mappingForEntityForName:@"Binder" inManagedObjectStore:managedObjectStore]; 


NSString *pathPattern = @"rest/case/list"; 

[router.routeSet addRoute:[RKRoute routeWithName:@"getCases" pathPattern:pathPattern method:RKRequestMethodGET]]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:caseEntityMapping pathPattern:pathPattern keyPath:@"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

[_objectManager addResponseDescriptor:responseDescriptor]; 


pathPattern = @"rest/binder/list/:caseId"; 

[router.routeSet addRoute:[RKRoute routeWithRelationshipName:@"binders" objectClass:[PFCase class] pathPattern:pathPattern method:RKRequestMethodGET]]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:binderEntityMapping pathPattern:pathPattern keyPath:@"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

[_objectManager addResponseDescriptor:responseDescriptor]; 

我用[_objectManager getObjectsAtPathForRouteNamed:@"getCases" object:nil parameters:nil success:… failure:…]得到這些個案,然後成功的塊裏面,我通過執行使用[_objectManager requestWithPathForRelationship:@"binders" ofObject:mappedCase method:RKRequestMethodGET parameters:nil]創建的請求操作得到了每種情況的粘合劑。

但是沒有粘合劑的映射結果。我錯過了什麼嗎?

回答

0

我不得不將外鍵RKConnectionDescription添加到綁定器實體映射中,這需要我爲Binder實體創建一個名爲caseId的新屬性。

有沒有辦法做到這一點,而不必創建一個新的屬性?另外,什麼是關鍵路徑RKConnectionDescription何時需要?