我有两个实体:Contact
和Listing
。 A User
可以将特定的Listing
标记为其Favourite
。 我正确设置了关系,并且我可以为User
添加Listing
作为Favourite
。Restkit addConnectionForRelationship挣扎
我面临的问题是API方面。它给我的关系只有id
s:contact_id
和listing_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];
什么版本的RestKit?显示您收到的JSON以及您当前使用的映射。 – Wain
收到用户时,收藏夹是否始终嵌套?所以你只有一个响应描述符? – Wain
是的,收藏夹总是嵌套。并且我有一个响应描述符描述符 –