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

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

问题描述

我有一个问题(理解问题坦诚)与NSFetchedResultsController和新的NSOrderedSet关系在iOS 5中可用。

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在Core Data模型/存储中都是NSManagedObjects。 Drawer socks 关系是许多 Sock 。想法是袜子在抽屉里按特定的顺序。 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;    
}

当我运行这个,我得到以下errror: ***由于未捕获异常而终止应用程序NSInvalidArgumentException,原因:此处不允许有多个键

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 UITableView > socks 关系。我真的不想有一个排序顺序,但 NSFetchedResultsController ,这是一个伟大的组件坚持必须有一个。我如何告诉它使用抽屉实体上的袜子顺序。我不想让表格显示Drawer实体。

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.

编辑:因此,显示的袜子的表格视图只适用于一个抽屉。我只想让表视图遵守socks关系包含的顺序。

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.

Andrew

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

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