Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map followers/following relationship with Restkit

In my core data project I have a User entity with a followers/following relationship which destination is also the User entity.

To get the info of a given user I call an endpoint with this structure: user/:id/ To fetch his followers/following users I call: /user/:id/followers

I have added the following routes to the RKObjectManager:

[manager.router.routeSet addRoute:[RKRoute routeWithRelationshipName:@"followers" objectClass:[User class] pathPattern:@"user/:id/followers" method:RKRequestMethodGET]];

[manager.router.routeSet addRoute:[RKRoute routeWithRelationshipName:@"following" objectClass:[User class] pathPattern:@"user/:id/following" method:RKRequestMethodGET]];

Then I call:

[[RKObjectManager sharedManager] getObjectsAtPathForRelationship:@"followers" ofObject:user parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    
}];

But RestKit cannot perform the mapping. I guess I'm missing adding to relationship information to the RKEntityMapping of the User entity. I've tried different approached but none successful. I appreciate any help on this issue.

Update

Here is the User entity mapping:

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"id":                 @"id",
     @"name":               @"name",
     @"user_name":          @"username"}];

And the descriptior:

[RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                  method:RKRequestMethodGET
                                             pathPattern:@"user/:id/followers"
                                                 keyPath:nil
                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]

I guess I should add something to the userMapping referencing followers/following but I haven't been able to figure it out yet. Thanks.

like image 875
fdezjose Avatar asked Sep 26 '13 10:09

fdezjose


1 Answers

This seems to be a limitation / bug in RestKit. You may want to raise this on the issues list in RestKit github page.

Your simplest fix should be to connect the relationship in the success block using the items provided in the mappingResult and the source user object.

like image 165
Wain Avatar answered Oct 13 '22 01:10

Wain