0

CoreDataデータベースを使用しています。エンティティAlbumがあり、多くのエンティティSpecificImageがあります(これらの画像のセットを取得するためのキーは@ "specificImages"です)id = 1のAlbumを取得し、添付されたSpecificImagesをidで並べ替えます。私は試した

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

しかし、私はエラーが発生します

キャッチされなかった例外'NSInvalidArgumentException'が原因でアプリを終了しています、理由:'to-manyキーはここでは許可されていません'

これが私のコードです:

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

4

2 に答える 2

4

Albumに多数のSpecificImagesがある場合、SpecificImageからAlbum(SpecificImageの1つのAlbumオブジェクト)への逆の関係もあると思います。

ここでは、最初にすべてのアルバムではなく、必要なアルバムIDのすべてのSpecificImagesを検索し、最終的にそれらのSpecificImageオブジェクトの1つを作成して、アルバムオブジェクトを取得します。

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を持つ必須のアルバムです。

于 2012-07-09T06:15:38.110 に答える
1

これを行う方法はありません。この問題がありました。しかし、フェッチ要求を実行した後、ID でソートする必要がありました。

于 2012-07-13T12:48:56.697 に答える