我有兩個實體: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
是的,收藏夾總是嵌套。並且我有一個響應描述符描述符 –