I few questions regarding RestKit and foreign key relationship mapping.
Before starting with my questions here is what is in place and working properly.
- Core Data model
Favorite has the following attributes :
placeIds: transformable type that hold the place ids that are define as user's favorite places.
- RestKit
a. User data mapping
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[entityMapping addAttributeMappingsFromDictionary:@{@"id":@"userId"}];
entityMapping.identificationAttributes = @[ @"userId" ];
[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"favorites" toKeyPath:@"favorites" withMapping:[AOFavorite mapping]]];
[entityMapping setAssignsDefaultValueForMissingAttributes:YES];
[entityMapping setAssignsNilForMissingRelationships:YES];
b. Favorite Data mapping
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[entityMapping addAttributeMappingsFromDictionary:@{@"placeIds":@"placeIds"}];
entityMapping.identificationAttributes = @[ @"userId" ];
[entityMapping addConnection:[AOFavorite placesConnection]];
[entityMapping setAssignsDefaultValueForMissingAttributes:YES];
[entityMapping setAssignsNilForMissingRelationships:YES];
+ (RKConnectionDescription *)placesConnection
{
NSEntityDescription *entity = [NSEntityDescription entityForName:[self entityName]
inManagedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
NSRelationshipDescription *places = [entity relationshipsByName][@"places"];
return [[RKConnectionDescription alloc] initWithRelationship:places attributes:@{ @"placeIds": @"placeId" }];
}
c. Place Data mapping
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName]
inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[entityMapping addAttributeMappingsFromDictionary:@{@"id":@"placeId"}];
entityMapping.identificationAttributes = @[ @"placeId" ];
[entityMapping setAssignsDefaultValueForMissingAttributes:YES];
[entityMapping setAssignsNilForMissingRelationships:YES];
User entity has the logged in user object correctly fit into persistent store.
Since the API provide User's favorite places with an array of ids, they are stored into User > Favorite > placeIds
--
I'm using the following method to request the API's object for the Favorite's place relationship with the Route defined as is:
[[[[RKObjectManager sharedManager] router] routeSet] addRoute:[RKRoute routeWithRelationshipName:@"favorite/places"
objectClass:[AOFavorite class]
pathPattern:@"user/favorite/places"
method:RKRequestMethodGET]];
[[RKObjectManager sharedManager] getObjectsAtPathForRelationship:@"favorite/places"
ofObject:[[AOUser currentUser] favorites]
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
- Problem and question
a. getObjectsAtPathForRelationship has the places correctly created into the Place entity but the Favorite > Place relationship isn't linked.
b. what is the purpose of having the placeIds into Favorite entities. I have seen SO question/response regarding Foreign key mapping stating that this is the way to do it. Can someone explained why ?