使用托管对象中的集合作为获取结果控制器的数据

发布于 2024-11-02 03:38:58 字数 469 浏览 1 评论 0原文

我想制作一个导航界面,用户可以点击一个单元格并出现一个与前一个相同的新导航控制器。我的托管对象具有以下结构:

name (string)
orderId (int)
orderBy (string, a key path indicating what to order the table with)
dateCreated (date)
items (a relationship pointing to the items for the next table)

在点击具有非零项目的项目时,下一个控制器将获取对点击项目的引用,并使用其“items”、“orderBy”和“orderId”来构造获取的结果控制器(及其项目作为数据)和排序描述符(使用 orderBy 和 OrderId)。

我如何告诉获取的结果控制器使用项目返回的 NSSet 作为其数据?我可以使用谓词将结果限制为仅一个对象的项目吗?谢谢

I want to make a Navigation interface, where a user can tap on a cell and have a new navigation controller the same as the previous one come up. My managed objects have the following structure:

name (string)
orderId (int)
orderBy (string, a key path indicating what to order the table with)
dateCreated (date)
items (a relationship pointing to the items for the next table)

On tapping an item with a non-nil items, the next controller get's a reference to the tapped item and use its "items", "orderBy" and "orderId" to construct a fetched results controller (with its items as data) and a sort descriptor (using the orderBy and OrderId).

How can I tell the fetched results controller to use the NSSet returned by items as its data? Can I use a predicate to limit results to the items of just one object? Thanks

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

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

发布评论

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

评论(1

原野 2024-11-09 03:38:58

您的项目需要具有反向多对一关系。我们称这种关系为“父母”。

在 tableView 委托的 didSelectRowAtIndexPath: 中,启动一个新的视图控制器,并在所述 indexPath 处传递“父”对象。您将需要创建一个访问器来在下一个视图控制器中保存所述对象。

正如您正确猜测的那样,您可以创建一个谓词,以便您的 fetchedResultsController 只返回所述“项目”。让您获取的结果仅搜索“item”实体。

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parentObject];
[fetchRequest setPredicate:resultsPredicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:[parentObject valueForKey:@"orderBy"] ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

基本上,您正在搜索与您选择的对象具有父关系的所有“项目”。

让我知道我是否正确描述了它(如果我没有正确描述,很抱歉,但我自己这样做,并且可以指出苹果的示例)。

编辑:苹果示例代码

http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008405

http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008913

Your items need to have a reverse many-to-one relationship. Lets call that relationship "parent".

In the didSelectRowAtIndexPath: of your tableView delegate, initiate a new view controller, and pass the "parent" object at said indexPath. You will need to create an accessor to hold said object in the next view controller.

As you properly guessed, you can make a predicate so that your fetchedResultsController only returns said "items". Make your fetched results only search for the "item" entity.

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parentObject];
[fetchRequest setPredicate:resultsPredicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:[parentObject valueForKey:@"orderBy"] ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

Basically, you are searching for all of the "items" that have a parent relationship to the object you selected.

Let me know if I described it properly (sorry if I didn't, but I do this myself, and can point to an Apple example).

EDIT: Apple Sample Code

http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008405

http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008913

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