2013-11-27 113 views
0

我有两个实体:ContactListing。 A User可以将特定的Listing标记为其Favourite。 我正确设置了关系,并且我可以为User添加Listing作为FavouriteRestkit addConnectionForRelationship挣扎

我面临的问题是API方面。它给我的关系只有id s:contact_idlisting_id

如何设置RestKit来映射Favourite的对象中定义的关系,该关系是从服务器获得的,该服务器只给出联系人和列表的两个对象id s?

Restkit版本:0.20.3

JSON用于收藏:

{ 
     "contact_id": "1", 
     "created_at": "2013-11-06T15:02:21.056Z", 
     "id": "2", 
     "listing_id": "3", 
     "updated_at": "2013-11-06T15:02:21.056Z" 
} 

JSON用于联系人:

{ 
    "id": 1, 
    "favorites": [ 
     { 
      "contact_id": "1", 
      "created_at": "2013-11-06T15:02:21.056Z", 
      "id": "2", 
      "listing_id": "3", 
      "updated_at": "2013-11-06T15:02:21.056Z" 
     } 
    ], 
    "first_name": Max, 
} 


////////// 
// Contact 
////////// 
RKEntityMapping *contactMapping = [RKEntityMapping mappingForEntityForName:@"PBContact" inManagedObjectStore:managedObjectStore]; 
contactMapping.identificationAttributes = @[ @"object_id" ]; 
[contactMapping addAttributeMappingsFromDictionary:@{ @"id": @"object_id" }]; 
[contactMapping addAttributeMappingsFromArray:@[ @"given_name", @"address", @"birthday", 
               @"city", @"company_name", @"country", 
               @"email", @"family_name", @"gender", 
               @"mobile_number", @"note", @"phone_number", 
               @"state", @"zip_code", @"created_at", @"updated_at" ]]; 


////////// 
// Listing 
////////// 
RKEntityMapping *listingMapping = [RKEntityMapping mappingForEntityForName:@"PBListing" inManagedObjectStore:managedObjectStore]; 
listingMapping.identificationAttributes = @[ @"object_id" ]; 
[listingMapping addAttributeMappingsFromDictionary:@{ @"id": @"object_id", @"description": @"descriptions" }]; 
[listingMapping addAttributeMappingsFromArray:@[ @"address", @"name", @"bathrooms", @"bedrooms", 
               @"city", @"country", @"price", @"title", 
               @"zip_code", @"latitute", @"longitude", @"status", 
               @"total_area", @"year_built", @"property_type", @"listing_type", 
               @"size", @"lot_size", @"parking_spaces", @"view", 
               @"state", @"note", @"monthly_rent", @"created_at", @"updated_at" ]]; 


////////// 
// Relationships 
////////// 

[contactMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"favoriteListings" withMapping:listingMapping]]; 

[listingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"prospects" withMapping:contactMapping]]; 

响应描述

RKResponseDescriptor *contactResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:contactMapping 
                         method:RKRequestMethodAny 
                        pathPattern:@"api/v1/contacts" 
                         keyPath:nil 
                        statusCodes:statusCodes]; 
+0

什么版本的RestKit?显示您收到的JSON以及您当前使用的映射。 – Wain

+0

收到用户时,收藏夹是否始终嵌套?所以你只有一个响应描述符? – Wain

+0

是的,收藏夹总是嵌套。并且我有一个响应描述符描述符 –

回答

0

你不需要(并应该删除E):

[listingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"prospects" withMapping:contactMapping]]; 

因为关系已经被从另一侧配置在contactMapping。它的外观不应该有任何其他变化。

为什么有3个IDS这是不完全清楚:

"contact_id": "1", 
"id": "2", 
"listing_id": "3", 

我假设contact_id总是匹配外接触的收藏夹中嵌套,并且不需要。而那id是有用的信息(所以你的映射是正确的)。

在您的核心数据模型中,favoriteListingsprospects应该是彼此相反的。


根据您的最新评论,我认为您需要创建一个新的不同的列表映射。该映射仅包含@"listing_id" : @"object_id"。这可以链接到现有的上市实体,也可以创建一个可以稍后填写的占位符。这是您应该用作contactMapping上的关系的映射。

+0

该id是来自服务器的Favorite对象的ID。 (我没有CoreData中的这个对象)并且contact_id和listing_id告诉我联系人1将列表3设置为它的最爱。因此,Restkit应该将ID为3的列表添加到Contact的favouriteListings中。 –

+0

房源详情来自其他请求?你有列表对象,你需要连接它们(外键映射)? – Wain

+0

我通过/ listings获取我的联系人详细信息通过/列表 我会通过联系人中的嵌套收藏夹或通过/收藏夹 –