NSFetchedResultsController 和 NSOrderedSet 关系 [英] NSFetchedResultsController and NSOrderedSet relationships

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

问题描述

我在使用 NSFetchedResultsController 和 iOS 5 中可用的新 NSOrderedSet 关系时遇到了问题(老实说是理解问题).

I am having an issue (understanding issue to be honest) with NSFetchedResultsController and the new NSOrderedSet relationships available in iOS 5.

我有以下数据模型(好吧,我真正的不是抽屉和袜子的!)但这只是一个简单的例子:

I have the following data-model (ok, my real one is not drawer's and sock's!) but this serves as a simple example:

Drawer 和 Sock 都是核心数据模型/存储中的 NSManagedObjects.在 Drawer 上,socks 关系是 ordered to-manySock 的关系.这个想法是袜子按特定顺序放在抽屉里.在 Sock 上,drawer 关系与 socks 关系相反.

Drawer and Sock are both NSManagedObjects in a Core Data model/store. On Drawer the socks relationship is a ordered to-many relationship to Sock. The idea being that the socks are in the drawer in a specific order. On Sock the drawer relationship is the inverse of the socks relationship.

在 UIViewController 中,我正在绘制基于这些实体的 UITableView.我正在使用 NSFetchedResultsController 为表格提供内容.

In a UIViewController I am drawing a UITableView based on these entities. I am feeding the table using a NSFetchedResultsController.

- (NSFetchedResultsController *)fetchedResultsController1 {
    if (_fetchedResultsController1 != nil) {
        return _fetchedResultsController1;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sock" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"drawer.socks" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    self.fetchedResultsController1 = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil cacheName:@"SocksCache"];
    self.fetchedResultsController1.delegate = self;

    return _fetchedResultsController1;    
}

当我运行这个时,我得到以下错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

When I run this, I get the following errror: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

这对我来说很有意义,因为该关系是一个 NSOrderedSet 而不是为了排序目的而进行比较的单个实体.

This makes sense to me, as the relationship is a NSOrderedSet and not a single entity to compare against for sorting purposes.

我想要实现的是让 Socks 按照 socks 关系中指定的顺序出现在 UITableView 中.我真的不想有一个排序顺序,但是 NSFetchedResultsController,这是一个很棒的组件,它坚持必须有一个.我怎么能告诉它使用抽屉实体上的袜子顺序.我根本不希望表格显示抽屉实体.

What I want to achieve is for the Socks to appear in the UITableView in the order specified in the socks relationship. I don't really want to have a sort order but NSFetchedResultsController, which is a great component is insisting there has to be one. How can I tell it to use the socks order on the Drawer entity. I don't want the table to show Drawer entities at all.

注意:我仅在 iOS5 应用程序中使用它,因此可以使用有序关系.

Note: I am using this within an iOS5 application only, so ordered relationships are available.

任何可以为我提供任何方向的人,将不胜感激.感谢您抽出宝贵时间.

Anyone that can offer me any direction, would be greatly appreciated. Thanks for your time.

所以显示袜子的 tableview 只为一个抽屉这样做.我只希望表视图遵守袜子关系包含的顺序.我不确定应该设置什么排序标准以确保发生这种情况.

So the tableview that display's socks does so for just one drawer. I just want the table view to honor the order that the socks relationship contains. I'm not sure what to set the sort criteria to make sure that happens.

推荐答案

Damien,

你应该让你的 NSFetchRequest 使用有序集合的数组形式.它将运行良好.您的控制器需要一个属性来排序.因此,您还需要指定它.

You should just make your NSFetchRequest use the array form of the ordered set. It will operate fine. Your controller needs an attribute to sort on. Hence, you'll need to specify that too.

安德鲁

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

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