Restkit addConnectionForRelationship苦苦挣扎

时间:2013-11-27 15:45:29

标签: connection restkit relationship restkit-0.20

我有两个实体:ContactListingUser可以将特定Listing标记为Favourite。 我正确设置了关系,并且我可以为Listing添加Favourite User

对我来说问题是API方面。它只向我提供了关系的idcontact_idlisting_id

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

Restkit版本:0.20.3

JSON for Favorite:

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

1 个答案:

答案 0 :(得分:0)

您不需要(并且应该删除):

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

因为已经在contactMapping的另一侧配置了关系。看起来不应该有任何其他变化。

目前还不完全清楚为什么有3个ID:

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

我假设contact_id始终匹配收藏夹嵌套的外部联系人,而不是必需的。并且id是有用的信息(因此您的映射是正确的)。

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


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