观察 NSTreeController 绑定的 CoreData 实体以进行插入/删除

发布于 2024-08-03 16:15:17 字数 455 浏览 9 评论 0原文

我正在创建自己的可绑定自定义树视图。为此,我想观察 NSTreeController 来更新其项目的多对多关系。 NSTreeController 绑定到 CD 管理对象上下文。每个深度级别都有自己的 CD 实体,具有parent/children/isLeaf 属性。我需要在视图中保持相同的层次结构(以及项目子项的顺序)。如果插入了某些东西,我将开始观察它的子属性。当然,我需要知道新插入的对象的索引路径。

我想确切地知道插入/删除了什么,这样我就可以开始观察它的子项插入/删除情况。据我了解,执行此操作的标准方法是在observeValue:forKey ...中查找“更改”NSDictionary,但更改为NULL。

我知道这是一个很长的错误,但是有一些好的解决方法吗?我已经看到一些示例的视图保存数组,然后当模型更改时,您可以比较差异。对于树视图来说更复杂。这种方式还会浪费内存和 CPU 周期。 我正在测试一种解决方法。它只是有效,所以我不会描述它。

I am creating my own bindable custom treeview. For that I would like to observe NSTreeController for updates to its items' to-many-relationships. NSTreeController is bound to CD managed object context. Every depth level has its own CD Entity with parent/children/isLeaf properties. I need to maintain same hierarcy in the view (and order of items children). If something is inserted, i will start observing its children property. And i need to know indexpath for the newly inserted object ofcourse.

I would like to know exactly what was inserted/removed, so i can start observing it for insertions/removals to its children. As i understand, standard way to do this is looking into "change" NSDictionary inside observeValue:forKey..., but changes are NULL.

I know that this is a long time bug but is there some GOOD workaround for it? I've seen that some examples' views save arrays and then when the model changes, you compare for differences. Its more complicated for treeview. Also this way wastes memory and CPU cycles.
I have one workaround that i am testing. It just kind of works so i will not describe it yet.

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

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

发布评论

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

评论(1

知你几分 2024-08-10 16:15:17

如何子类化 NSTreeController 并实现其插入/删除方法。例如这样的事情。

- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath {

      // ... code to update your object relationships .. 
      // ... Take care here if you update any variables in your model (eg a sortindex) that would trigger KVO in the NSTreeController.  In those case you need to make the updates without triggering KVO by using setPrimitiveValue:forKey or get an infinite loop

      [super insertObject:object atArrangedObjectIndexPath:indexPath];

}

How about subclassing NSTreeController and implementing its insert/remove methods. Something like this for example.

- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath {

      // ... code to update your object relationships .. 
      // ... Take care here if you update any variables in your model (eg a sortindex) that would trigger KVO in the NSTreeController.  In those case you need to make the updates without triggering KVO by using setPrimitiveValue:forKey or get an infinite loop

      [super insertObject:object atArrangedObjectIndexPath:indexPath];

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