使用 NSFetchedResultsController 对多对多关系进行排序

发布于 2024-09-26 14:45:21 字数 2260 浏览 3 评论 0原文

我尝试在我的应用中使用 NSFetchedResultsController,但在对数据进行排序时遇到问题。当尝试使用比实体低两级的关系对结果进行排序时,出现以下错误:

* 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因: “此处不允许使用多对多密钥”

我的数据模型是这样设置的:

项目<<--->类别<--->>排序顺序 <<--->商店

换句话说:每件商品都属于一个类别。对于包含特定类别的每个商店,类别可以具有不同的排序顺序。

因此,我正在创建一个获取请求来查找某个商店的所有商品,并希望使用类别名称作为部分来显示结果,并按排序顺序进行排序。

当我执行提取(下面最后一行)时,我收到上述错误。

 NSManagedObjectContext* context = [appDelegate managedObjectContext];
 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(status != %d) AND (ANY category.sort.include == YES) AND (ANY category.sort.store == %@)", ItemStatusDefault, store];

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:context];
 [fetchRequest setEntity:entity];
 [fetchRequest setPredicate:predicate];

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.sort.order" ascending:YES];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 [fetchRequest setSortDescriptors:sortDescriptors];
 [sortDescriptors release];
 [sortDescriptor release];

 self.resultsController = [[NSFetchedResultsController alloc]
             initWithFetchRequest:fetchRequest
             managedObjectContext:context
             sectionNameKeyPath:@"category.name"
             cacheName:nil];
 [fetchRequest release];

 NSError *error;
 BOOL success = [self.resultsController performFetch:&error]; 

如果我将排序更改为类别名称,那么它就会起作用。

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES];

如何让 NSSortDescriptor 按排序顺序进行排序?

更新: 所以看来这是不可能的。我收到了创建瞬态属性并对其进行排序的建议,但是 苹果文档明确指出

您无法使用谓词获取 基于瞬态属性

我的结论是我不能立即使用 NSFetchedResultsController 。我需要访问 NSFetchResultsController 提供的对象数组并在内存中排序,或者设置我自己的获取请求并跳过 NSFetchedResultsController。

I'm trying to use the NSFetchedResultsController in my app, but have a problem to sort my data. I get the following error when trying to sort the result using a relationship that is two levels down from the entity:

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

My data model is set up this way:

Item <<---> Category <--->> SortOrder
<<---> Store

In other words: Each item belongs to one category. Categories can have different sort orders for each store that includes a certain category.

So, I'm creating a fetch request to find all items for a certain store and would like to present the result using category names as sections, and sorted on the sort order.

When I perform the the fetch (last line below), I get the above error.

 NSManagedObjectContext* context = [appDelegate managedObjectContext];
 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(status != %d) AND (ANY category.sort.include == YES) AND (ANY category.sort.store == %@)", ItemStatusDefault, store];

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:context];
 [fetchRequest setEntity:entity];
 [fetchRequest setPredicate:predicate];

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.sort.order" ascending:YES];
 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 [fetchRequest setSortDescriptors:sortDescriptors];
 [sortDescriptors release];
 [sortDescriptor release];

 self.resultsController = [[NSFetchedResultsController alloc]
             initWithFetchRequest:fetchRequest
             managedObjectContext:context
             sectionNameKeyPath:@"category.name"
             cacheName:nil];
 [fetchRequest release];

 NSError *error;
 BOOL success = [self.resultsController performFetch:&error]; 

If I change the sorting to, say, category names, it works.

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES];

How can I get the NSSortDescriptor to sort on the sort order?

UPDATE:
So it seems this is not possible. I got a suggestion to create a transient property and sort on that, but Apple documentation clearly states

You cannot fetch using a predicate
based on transient properties

My conclusion here is that I cannot use NSFetchedResultsController out of the box. I need to either access the array of objects the NSFetchResultsController gives me and sort in memory, or setup my own fetch requests and skip NSFetchedResultsController.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-10-03 14:45:21

iOS 5 现在提供有序关系

https://developer.apple .com/LIBRARY/ios/releasenotes/DataManagement/RN-CoreData/index.html

更新:
链接更新

参考:“OS X v10.7 和 iOS 5.0 的核心数据发行说明”

iOS 5 provide now ordered relationships

https://developer.apple.com/LIBRARY/ios/releasenotes/DataManagement/RN-CoreData/index.html

UPDATE:
Link updated

Reference : "Core Data Release Notes for OS X v10.7 and iOS 5.0"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文