核心数据一对多排序 [英] Core Data one-to-many sorting

查看:84
本文介绍了核心数据一对多排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 CoreData 对象实体,其属性为 @property(非原子,保留)NSSet * ImagesSet;

I have CoreData object "Entity" with @property (nonatomic, retain) NSSet *ImagesSet;

内部ImagesSet中有许多 ImageEntity对象。

Inside ImagesSet are many "ImageEntity" objects.

ImageEntity具有 @property(非原子,保留)NSNumber * sortKey;

"ImageEntity" have @property (nonatomic, retain) NSNumber * sortKey;

如何使用sortKey字段排序的imagesSet为 Entity创建fetchRequest?

How create fetchRequest for "Entity" with imagesSet ordered by sortKey field?

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity"
                                          inManagedObjectContext:[[DataManager sharedInstance] mainObjectContext]];
[fetchRequest setEntity:entity];
NSString * predicate = some predicate string......;
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:predicate]];
[fetchRequest setFetchBatchSize:2500];
[fetchRequest setFetchLimit:25];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ImageEntity.sortKey" ascending:NO];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *frc = nil;

 frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                          managedObjectContext:mainObjectContext
                                            sectionNameKeyPath:nil
                                                     cacheName:cashName];

由于未捕获的异常 NSInvalidArgumentException,程序崩溃并显示消息 *****终止应用程序,原因:'此处不允许使用to-many键'**

Program crashes with message "***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'**"

推荐答案

获取实体对象,没有任何方法可以控制相关 ImageEntity 对象作为获取的顺序。相反,您有几个选择:

When you fetch Entity objects, there is no way to control the order of the related ImageEntity objects as part of the fetch. You have a few options instead:


  1. 在数据模型中指定关系是有序的。该关系将表示为 NSOrderedSet 而不是 NSSet 。您将必须确保根据需要对集合进行排序(而不是使用 sortKey )。

  1. Specify in your data model that the relationship is ordered. The relationship will be represented as NSOrderedSet instead of NSSet. You will have to ensure that the set is ordered as you need (instead of using sortKey).

使关系保持无序状态( NSSet ),并在需要时对集合进行排序。例如,

Keep the relationship as unordered (NSSet), and sort the set when you need to. Eg.

NSSortDescriptor *imageSort = [NSSortDescriptor sortDescriptorWithKey:@"sortKey" ascending:NO];
NSArray *sortedImages = [myEntity.imagesSet sortedArrayUsingDescriptors:@[imageSort]];


  • 直接获取 ImageEntity 对象,使用谓词将结果过滤到相关的 Entity 对象和所需的排序描述符:

  • Fetch the ImageEntity objects directly, using a predicate to filter the results to the relevant Entity object, and the required sort descriptor:

    NSSortDescriptor *imageSort = [NSSortDescriptor sortDescriptorWithKey:@"sortKey" ascending:NO];
    NSFetchRequest *imageFetch = [NSFetchRequest fetchRequestWithEntityName:@"ImageEntity"];
    imageFetch.sortDescriptors = @[imageSort];
    imageFetch.predicate = [NSPredicate predicateWithFormat:@"entity == %@",myEntity];
    NSError *error;
    NSArray *sortedImages = [context executeFetchRequest:imageFetch error:&error];
    


  • 这篇关于核心数据一对多排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆