ZeNewb
ZeNewb

Reputation: 435

RestKit mapping unnested relationships to owning object

I am trying to associate a GET response of photos with their owning User object in Core Data using RestKit (i.e. to be able to call user.photos after the request and have them automagically mapped). However, I don't understand how I can set this up to do it automatically versus me having to use [user setPhotos:...] explicitly from the RKMappingResult

Is there a way to accomplish this automatically using mappings and response descriptors?

Route / Response Descriptor:

RKRoute *userPhotos = [RKRoute routeWithRelationshipName:@"photos" objectClass:[User class] pathPattern:@"/api/v1/photos" method:RKRequestMethodGET];
[[[[APIClient objectManager] router] routeSet] addRoute:userPhotos];

RKResponseDescriptor *photosDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:self.photoMapping   
                                                                                      method:RKRequestMethodGET
                                                                                 pathPattern:nil
                                                                                     keyPath:kJsonPhotos
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[[APICacheStore sharedStore] objectManager] addResponseDescriptorsFromArray:@[photosDescriptor]];

Code:

+ (void)getPhotosForUser:(User *)user success:(void (^)())success failure:(void (^)(NSString *errorMsg))failure {
  NSAssert(user, @"User must not be nil when we attempt to fetch their photos.");
  [[APIClient objectManager] getObjectsAtPathForRelationship:@"photos" ofObject:user parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    [user setPhotos:[NSSet setWithArray:[mappingResult array]]];
    success();
  } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    failure(error.localizedDescription);
  }];
}

Original JSON:

{
  "meta": {
    "total": 2,
    "offset": 0,
    "limit": 10
  },
  "photos": [
    {
      "id": "d8e1ca59-9f50-4963-8e7e-0b0c8de4bed6",
      "name": "Mary at the mall",
      "updated_at": "2013-12-11T10:16:41.000Z",
      "completed_at": null,
      "created_at": "2013-12-11T10:16:41.000Z",
      "image": "images/is9912zh/image.jpeg"
    },
    {
      "id": "1c75273a-7675-43a9-9b62-3d4236439ec6",
      "name": "John at the park",
      "updated_at": "2013-12-11T10:16:41.000Z",
      "completed_at": null,
      "created_at": "2013-12-11T10:16:41.000Z",
      "image": "images/aj92j127/image.jpeg"
    }
  ]
}

Upvotes: 3

Views: 88

Answers (1)

Wain
Wain

Reputation: 119041

Not with the information you appear to have available and the way that you're sending the request.

If the path pattern was @"/api/v1/photos/##USER_ID##" then the route would give enough information for a foreign key mapping. If the returned JSON had the user id, that would give enough information for a foreign key mapping.

Technically, the operation has all of the information that is required to populate the relationship but RestKit doesn't (currently) attempt to do it. You could look at implementing this and submitting a pull request.

Upvotes: 1

Related Questions