从阵列控制器中删除实体时,如何删除实体? [英] How do I delete an entity when removing it from an array controller?

查看:135
本文介绍了从阵列控制器中删除实体时,如何删除实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与其他两个实体(例如Department和Team)相关的受管对象模型中有实体(例如Employee)。这两个关系是一对多的(即员工必须有一个部门和一个团队,团队和部门有很多员工)。这两个可能或可能不重叠(例如,一个团队可能由人力资源,会计和IT部门的员工组成,或者它可能由一个部门的几个员工组成)。

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    

我有两个NSArrayController为两个NSTableViews,一个Department表和一个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?

推荐答案

NSArrayController 在使用Core Data时有两种不同的行为。如果配置为直接从托管对象上下文中获取对象,则在删除对象时将删除对象。

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.

如果绑定 contentSet 到另一个控制器,就像它在这种情况下,默认行为是简单地从关系中删除对象。如果你想要删除它,虽然有一个删除对象在删除绑定选项,这将产生你想要的结果。

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.

这篇关于从阵列控制器中删除实体时,如何删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆