2012-05-30 5 views
0

코어 데이터베이스를 사용합니다. 많은 엔티티 SpecificImage (이러한 이미지 세트를 가져올 키는 @ "specificImages"입니다.) id = 1 인 앨범을 가져오고 id로 연결된 SpecificImages를 정렬하려고합니다. 나는NSSortDescriptor 대단위 관계

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"specificImages.id" ascending:YES]; 

을 시도하지만 난 오류가 여기에

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

내 코드입니다 : 내가있는 NSArray * 객체 = [컨텍스트를 실행하려고 할 때

NSString *entityName = kEntityName; 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];; 
    NSManagedObjectContext *context = appDelegate.managedObjectContext; 
    NSEntityDescription *entityDesctiption = [NSEntityDescription 
               entityForName: entityName 
               inManagedObjectContext:context]; 

    // find object 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %d", CurrentCategoryItemID]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"specificImages.id" ascending:YES]; 
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
    [request setEntity:entityDesctiption]; 
    [request setPredicate:predicate]; 
    NSArray *objects = [context executeFetchRequest:request error:nil]; 
    [request release]; 
    if (objects == nil) 
    { 
     NSLog(@"there was an error"); 
     return; 
    } 
    else if ([objects count] == 0) 
    { 
     return; 
    } 
    NSManagedObject *object = [objects objectAtIndex:0]; 

그것은 예외를 throw executeFetchRequest : 요청 오류 : nil];

답변

4

앨범에 많은 SpecificImage가있는 경우 SpecificImage와 Album (SpecificImage의 1 Album 개체)의 반대 관계가 있다고 가정합니다.

여기서는 모든 앨범이 아닌 필요한 앨범 ID의 모든 SpecificImages를 찾았으며 최종 형식 인 SpecificImage 개체 중 하나에서 앨범 개체를 다시 가져옵니다.

NSString *entityName = @"SpecificImage"; 
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];; 
NSManagedObjectContext *context = appDelegate.managedObjectContext; 
NSEntityDescription *entityDesctiption = [NSEntityDescription 
              entityForName: entityName 
              inManagedObjectContext:context]; 

// find object 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album.id == %d", CurrentCategoryItemID]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES]; 
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
[request setEntity:entityDesctiption]; 
[request setPredicate:predicate]; 
NSArray *specificImagesArray = [context executeFetchRequest:request error:nil]; 
[request release]; 
if (objects == nil) 
{ 
    NSLog(@"there was an error"); 
    return; 
} 
else if ([objects count] == 0) 
{ 
    return; 
} 
Album *album = [(SpecificImage *)[specificImagesArray objectAtIndex:0] album]; 

specificImagesArray 당신이 찾고있는 specificImages의 배열 및 앨범 ID = CurrentCategoryItemID하여 필요한 앨범이다.

+0

첨부 된 이미지를 정렬하고 이미지를 정렬 한 다음 앨범을 반환하지 않으려합니다. 그래서 [NSSortDescriptor sortDescriptorWithKey : @ "specificImages.id"와 같은 것을 사용해야합니다 : 오름차순 : YES]; –

+0

specificimages는 nsset 객체입니다. 그래서 id가 없습니다 .. fetchRequest에 sortdescriptor를 추가하면 결과 객체의 키 값을 비교하여 결과 배열을 정렬합니다. –

+0

최선의 해결책은 무엇입니까? UR 문제에 대한 .. 거기에 하나 이상의 덜 효율적인 솔루션입니다 .. fetchRequest ..에 어떤 sortdescriptors를 포함하지 않을 것이다 당신이 원하는 앨범을 가져 와서 다음 해당 specifsimages nsset에 액세스하고 원하는대로 UR 욕구를 정렬 –

1

내 생각에, 어떻게 변형 할 것인가? 나는이 문제가 있었다. 하지만 fetch 요청을 실행 한 후에 id에 의해 sord를 써야했습니다.