使用 CoreData 自动持久从 NSArrayController 中删除对象

发布于 2024-08-15 17:44:46 字数 344 浏览 1 评论 0原文

我有一个 NSTableView,其中一列绑定到 NSArrayController,表示来自 NSManagedObjectContext 的实体集合。我还有一个“删除”按钮,连接到 NSArrayController 的删除操作,一切正常 - 表格显示数据,当我单击“删除”时,实体将从表格中删除等。

然而,这种删除似乎只发生在对象图中,从未持久化到磁盘。是否有任何标志或方法可以自动将此类更改持久保存到对象图,或者是为删除按钮引入新的 IBAction 并在控制器上显式调用第一个 remove 的唯一方法,并且然后在 NSManagedObjectContext保存

I have a NSTableView with one column bound to NSArrayController representing a collection of entities from a NSManagedObjectContext. I also have a "remove" button that is connected to NSArrayController's remove action, and it all works fine - table is showing data, when I click 'remove', the entity gets removed from the table etc.

However, this removal seems to be only happening in the object graph is never persisted to disk. Is there any flag or a way to persist such changes to object graph automatically or is the only way introducing new IBAction for the remove button and explicitely calling first remove on the controller and then save on NSManagedObjectContext?

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

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

发布评论

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

评论(1

忱杏 2024-08-22 17:44:46

您似乎在谈论两个不同的问题:

  1. “从上下文中删除”与“从集合中删除”以及
  2. 立即将更改保存到上下文。

如果您的内容数组绑定到其他源,您可以检查内容数组绑定的“删除时删除对象”选项。如果内容仅仅是您提供的实体的所有实例(即,未建立内容/数组绑定,并且只是获取所有 Foo 实例),我希望该对象被标记为删除。将更改保存到存储中最终会删除对象。

要立即保存更改,您可能需要自己的单独操作来传递“删除此对象并保存”操作。在这种情况下,您可以执行删除/删除,要求托管对象上下文处理PendingChanges,然后调用保存例程。

You appear to be talking about two separate issues:

  1. "Deleting from context" versus "removing from collection" and
  2. Saving changes to a context immediately.

If your content array is bound to some other source, you can check the "Deletes objects on remove" option for the content array binding. I would expect the object be marked for deletion if the contents is merely all instances of your supplied entity (ie, the content / array binding is not established and it's just fetching all Foo instances). Saving the changes to the store ultimately deletes the objects.

To save changes immediately, you'd probably need your own separate action through which to pass your "delete this object and save" action. In that case, you can perform your remove/delete, ask the managed object context to processPendingChanges, then call the save routine.

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