2013-01-02 2 views
2

내가 좋아하는, 중첩 된 자원 URL을 사용하여 앨범에서 사진을 얻기 위해 노력하고 있어요. 그러나 앨범 객체에 액세스 할 수 없으므로 album_id를 포함한 URL을 반환 할 수 없습니다. 어떻게 이것을 재정의/연장해야합니까? 내가 앨범 ID 제공 할 수없는 경우 물음표를 참고, 내가이 일을하려고했던 방법은 아래를 참조하십시오 : 스택 최대중첩 된 리소스가있는 AFRESTClient pathForEntity? 그것은이 작업을 수행 할 수있는 건조한 방법처럼 보인다</p> <pre><code>GET http://www.myapi.com/albums/:album_id/photos </code></pre> <p>AFRESTClient의 내 서브 클래스에서 pathForEntity 기능을 무시하는 것입니다 :

- (NSString *)pathForEntity:(NSEntityDescription *)entity { 

    NSString *path = AFPluralizedString(entity.name); 

    if ([entity.name isEqualToString:@"Photo"]) { 
    path = [NSString stringWithFormat:@"albums/%d/photos", ??]; 
    } 

    return path; 
} 

이상, 내가 PhotosViewController이있다는

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.title = self.currentAlbum.name; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Photo"]; 
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album == %@", self.currentAlbum]; 
    fetchRequest.predicate = predicate; 

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 
    self.fetchedResultsController.delegate = self; 
    [self.fetchedResultsController performFetch:nil]; 
} 
을 -viewDidLoad

감사!

+0

솔루션을 찾았습니까? – sbonkosky

답변

0

우리는 비슷한 계층 구조를 가지고 있으며 결국 AFRESTClient 하위 클래스의 pathForRelationship : forObject : 메소드를 구현합니다.

- (NSString *)pathForRelationship:(NSRelationshipDescription *)relationship 
         forObject:(NSManagedObject *)object 
{ 
    if ([object isKindOfClass:[Album class]] && [relationship.name isEqualToString:@"photos"]) { 
     Album *album = (Album*)object; 
     return [NSString stringWithFormat:@"/albums/%@/photos", album.album_id]; 
    } 
    return [super pathForRelationship:relationship forObject:object]; 
} 

이것은 당신이 당신의 모델의 toMany 관계 설정을 가정 한 것입니다 : 사진 및 앨범에 대한 귀하의 경우에는, 나는 그것이 같은 될 것이라고 상상하는 것입니다.