在具有跨越多个实体的Core Data绑定的Cocoa中实现parent>子查询 [英] Implementing parent->child drill down in Cocoa with Core Data bindings that span multiple entities
问题描述
我尝试以iTunes类型浏览器的风格为我的核心数据模型创建一个简单的界面。
I'm trying to create a simple interface to my core data model in the style of iTunes Genre browser.
我有一个模型设置,有三个实体: code> Genre< - >>艺术家< - >>
I have a model setup with three entities: Genre <-->> Artist <-->> Album
.
我想简单地将它们绑定到一个NSTableView,但它看起来好像访问儿童关系从一个NSArrayController不是KVC兼容。因此,我无法将所选的Genre对象传递给ArtistController。
I'd like to simply tie them each to a NSTableView, but it appears as though accessing children relationships from a NSArrayController is not KVC compliant. So, I'm having difficulty communicating the selected Genre objects to the ArtistController.
如何做到这一点?
编辑Posterity:我做错了几个错误。
- 子控制器需要通过自己的绑定知道managedObjectContext。
- 子控制器必须位于
Entity
模式中,而是作为NSMutableDictionary
类进行操作。 - 最后,子控制器不准备其数据。它通过
内容集
绑定从父级检索它。使用控制器键selection
,以及连接到子项的模型键路径。
- The child controller needs to know about the managedObjectContext through its own binding.
- The child controller must not be in
Entity
Mode, but rather operate as aNSMutableDictionary
class. - And, finally, the child controller does not prepare its data. It retrieves it from the parent, through the
Content Set
binding. Use the controller keyselection
, and the model key path that connects to the children.
phew 。 Brian的答案和此MacResearch教程
phew. Both Brian's answer and this MacResearch tutorial were helpful in determining my errors (and which parts I had right).
推荐答案
我可能会采取的方法是一个单独的NSArrayController为每个表视图,然后有一个数组控制器的内容基于另一个选择。
The approach I would probably take is to have a separate NSArrayController for each table view, then have the content of one array controller be based on the selection of another.
例如,你有表视图A显示可用流派的列表,所以它有一个数组控制器A的内容被挂接到您的托管对象上下文。
For example, say you have table view A that displays the list of available genres, so it has an array controller A whose content is hooked up to your managed object context.
然后,假设您有表视图B,其中显示了表A中选择的任何类型的可用艺术家。此表将有自己的数组控制器B,控制器B的数组被绑定,因此IB中的控制器键字段被设置为控制器A的选择键,其中艺术家是模型键(这假设您的类型实体具有名为艺术家到艺术家实体)。
Then, say you have table view B which shows the available artists for whichever genre is selected in table A. This table would have its own array controller B, and the content array for controller B is bound so the "controller key" field in IB is set to the "selection" key of controller A, with "artists" being the model key (this assumes your Genre entity has a to-many relationship named "artists" to the Artist entity).
然后,您可以将同样的原理应用到第三个表格视图+控制器,以显示所选艺术家的相册。
You can then apply the same principle to a third table view + controller to show the albums for the selected artist.
这种设置的通用术语是主 - 细节界面,并在Apple的文档此链接
The general term for this kind of setup is a "master-detail interface", and is outlined in Apple's docs at this link
这篇关于在具有跨越多个实体的Core Data绑定的Cocoa中实现parent>子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!