2012-05-30 35 views
0

我使用Core Data数据库。 我有实体相册,有很多实体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'

这里是我的代码:

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]; 

它抛出一个异常,当我尝试执行的NSArray *对象= [背景executeFetchRequest:request error:nil];

回答

4

我假设,如果您在Album中有很多SpecificImages,那么您也将具有从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通过比较所有结果对象中的th键的值来对结果数组进行排序。 –

+0

我所做的是最好的解决方案对于你的问题..还有一个效率更低的解决方案..这将不包括fetchRequest中的任何sortdescriptors ..或者你可以获取你想要的专辑,然后访问它的specifcimages nsset并将值分类到你的愿望 –

1

我想,没有任何变种,如何做到这一点。我有这个问题。但是在执行提取请求后,我不得不通过id来播放它。