从阵列控制器中删除实体时如何删除实体?
我在托管对象模型中有一个实体(例如员工),该实体与其他两个实体(例如部门和团队)相关。两种关系都是一对多的(即一名员工必须有一个部门和一个团队,团队和部门有很多员工)。两者可能重叠,也可能不重叠(例如,一个团队可能由人力资源、会计和 IT 部门的员工组成,或者可能仅由一个部门的多名员工组成)。
Department <-->> Employee <<--> Team
我有两个 NSArrayController 为两个 NSTableView、一个部门表和一个团队表提供数据。员工可以在部门之间和团队之间流动,没有任何问题,但我不知道如何删除(解雇)员工。
如果我向任一数组控制器发送一条删除消息,则该员工将被带出团队(例如),但仍留在部门中,并且对象图处于不一致状态。即使我在两个控制器上调用删除操作,该对象也不会被删除 - 它是孤立的,只是悬而未决。
最初我有部门和部门。 (员工实体的)团队关系设置为 Nullify 删除规则,但即使将一个或两个更改为级联也无济于事。
我是否需要覆盖阵列控制器上的删除:操作才能实际删除员工,或者我是否遗漏了一些非常明显的东西?
I have an entity (e.g. Employee) in a managed object model that is related to two other entities (e.g. Department and Team). Both relationships are one-to-many (i.e. an Employee must have one Department and one Team, Teams and Departments have many Employees). The two may or may not overlap (e.g. a team might be made up of employees from HR, Accounting & I.T. or it might jut consist of several employees from the one department).
Department <-->> Employee <<--> Team
I have two NSArrayControllers providing data for two NSTableViews, a Department table and a Team table. Employees can move between departments and between teams without any problems but I'm not sure how to delete (fire) the employee.
If I send either of the array controllers a remove message the employee is taken out of the team (for example) but left in the department and the object graph is in an inconsistent state. Even if I call the remove action on both controllers the object is not deleted - it is orphaned and just hangs around in limbo.
Originally I had the Department & Team relationships (of the Employee entity) set to a delete rule of Nullify but even changing one or both to cascade doesn't help.
Do I need to override the remove: action on the array controllers to actually delete the employee or am I missing something really obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用 Core Data 时,
NSArrayController
有两种不同的行为。如果将其配置为直接从托管对象上下文中直接获取对象,则在删除对象时它将删除这些对象。如果您将
contentSet
绑定到另一个控制器(听起来就像在这种情况下),默认行为是简单地从关系中删除该对象。但是,如果您想删除它,有一个“删除时删除对象”绑定选项,它将产生您想要的结果。The
NSArrayController
has two different behaviors when you're using Core Data. If it is configured to simply fetch objects directly from the managed object context, it will delete the objects when they are removed.If you're binding the
contentSet
to another controller, like it sounds like you are in this case, the default behavior is to simply remove the object from the relationship. If you want to delete it, though, there is a "deletes object on remove" binding option, which will produce the result you want.