I have two entities: Contact
and Listing
. A User
can mark a specific Listing
as its Favourite
.
I set up the Relationships correctly and I'm able to add a Listing
as a Favourite
for the User
.
The problem for me is the API side. It gives me only the id
s of the relationship: contact_id
and the listing_id
.
How can I set up RestKit to map the relationships defined in the Favourite
s object I get from the server which only gives me the two object id
s of a contact and a listing?
Restkit Version: 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 for Contact:
{
"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]];
Response Descriptor
RKResponseDescriptor *contactResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:contactMapping
method:RKRequestMethodAny
pathPattern:@"api/v1/contacts"
keyPath:nil
statusCodes:statusCodes];
You don't need (and should delete):
[listingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"prospects" withMapping:contactMapping]];
because the relationship is already being configured from the other side on the contactMapping
. There shouldn't need to be any other changes by the look of it.
It isn't entirely clear why there are 3 ids:
"contact_id": "1",
"id": "2",
"listing_id": "3",
I'm assuming that contact_id
always matches the outer contact that the favourites are nested in and isn't required. And that id
is the useful piece of information (so your mapping is correct).
In your Core Data model, favoriteListings
and prospects
should be the inverse of each other.
Based on your latest comments, I think you need to create a new and different listing mapping. This mapping only contains @"listing_id" : @"object_id"
. This will either link to an existing listing entity or create a placeholder that can be filled in later. This is the mapping that you should use as the relationship on contactMapping
.