Restkit addConnectionForRelationship苦苦挣扎

问题描述 投票:0回答:1

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

对我来说问题是API方面。它只给了我关系的ids:contact_idlisting_id

我如何设置RestKit来映射我从服务器获得的Favourites对象中定义的关系,它只给出了联系人和列表的两个对象ids?

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];
connection restkit relationship restkit-0.20
1个回答
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上的关系的映射。

© www.soinside.com 2019 - 2024. All rights reserved.