本文介绍了NSSortDescriptor对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用核心数据数据库.我有实体相册,其中有很多实体SpecificImage(获取这些图像的关键是@"specificImages")我想获取id = 1的Album并按id对附加的SpecificImages进行排序.我尝试过

I use Core Data database.I have Entity Album, that has many entities SpecificImage (key to get set of these images is @"specificImages")I want to get Album with id = 1 and to sort attached SpecificImages by id.I tried

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

但是我得到了错误

这是我的代码:

    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 * objects = [context executeFetchRequest:request error:nil];时会引发异常;

it throws an exception, when I try to execute NSArray *objects = [context executeFetchRequest:request error:nil];

推荐答案

我假设,如果相册"中有许多SpecificImages,那么从"SpecificImage"到相册"(SpecificImage中有1个相册"对象)也将成反比关系.

I am assuming, if you have many SpecificImages in Album then you will also have an inverse relationship from SpecificImage to Album ( 1 Album object in SpecificImage ).

在这里,我首先找到所需专辑ID的所有SpecialImages,而不是所有专辑,最后以这些SpecificImage对象之一的形式返回专辑对象.

Here I am first finding all SpecificImages for the required album id rather than all Albums and in the end form one of those SpecificImage object I will get the album object back.

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的数组,而Album是ID = CurrentCategoryItemID的必需相册.

The specificImagesArray is the array of specificImages you are looking for and album is your required album with id = CurrentCategoryItemID.

这篇关于NSSortDescriptor对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 21:05