iphone - NSFetchedResultsController 和 NSOrderedSet 关系

标签 iphone objective-c ios core-data ios5

我对 NSFetchedResultsController 和 iOS 5 中可用的新 NSOrderedSet 关系有疑问(老实说,理解问题)。

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

enter image description here

Drawer 和 Sock 都是 Core Data 模型/存储中的 NSManagedObjects。在 Drawer 上,socks 关系是与 Sock有序对多关系。想法是 socks 按特定顺序放在抽屉里。在 Sock 上,drawer 关系与 socks 关系相反。

在 UIViewController 中,我正在绘制基于这些实体的 UITableView。我正在使用 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;    
}

当我运行它时,出现以下错误:*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“此处不允许对多 key ”

这对我来说很有意义,因为关系是 NSOrderedSet 而不是用于排序目的的单个实体比较。

我想要实现的是 Sockssocks 关系中指定的顺序出现在 UITableView 中。我真的不想有一个排序顺序,但是 NSFetchedResultsController 是一个很棒的组件,它坚持必须有一个。我怎样才能告诉它在抽屉实体上使用 socks 订单。我根本不希望表格显示抽屉实体。

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

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

编辑:所以显示 socks 的 tableview 只对一个抽屉这样做。我只希望 TableView 遵守 socks 关系包含的顺序。我不确定如何设置排序标准以确保发生这种情况。

最佳答案

达米安,

您应该让您的NSFetchRequest 使用有序集的数组形式。它将运行良好。您的 Controller 需要一个属性来排序。因此,您也需要指定它。

安德鲁

关于iphone - NSFetchedResultsController 和 NSOrderedSet 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8758429/

相关文章:

iphone - 从 Xcode 更改按钮文本?

ios - 如何在使用选项卡 View Controller 的页面中将标题设置为第一个导航 Controller 。?

ios - 没有越狱检测

iphone - 发布 App 更新时,我在 bundle 路径中的数据是否会发生变化或被删除?

ios - Apple Carplay - 如何创建可播放容器?

ios - 将 JSON(日期)解析为 Swift

iphone - Objective-C 数据库连接

iphone - 在后台运行 iOS 应用程序

iphone - 如何修改现有的iOS ABRecord地址

iphone - Obj-C,迎合 'tap for more rows'并删除无核心数据?