【发布时间】:2014-11-17 04:49:59
【问题描述】:
假设我有一个名为 Player 的 CoreData 实体类型,它与名为 PlayerPurpose 的实体类型具有一对一关系 (purpose)。为了完整起见,假设我们在PlayerPurpose 中有一个反向关系,称为parentPlayer。考虑以下 swift 代码:
// Assume we already have a player object in a NSManagedObjectContext called context:
player.purpose = NSEntityDescription.insertNewObjectForEntityForName("PlayerPurpose",
inManagedObjectContext: context) as PlayerPurpose;
// Later in the code, we set the value to nil (or we could have replaced
// it with another call to insertNewObjectForEntityForName)
player.purpose = nil;
// What happens to the previous playerPurpose object within the Managed Object Context?
我的问题:当原始 playerPurpose 对象在数据中的唯一引用设置为 nil(或替换为另一个对象)时,托管对象上下文中的原始 playerPurpose 对象会发生什么?
这与关系删除规则并没有真正的关系,因为我没有明确删除任何对象——我将其从任何有意义的关系中删除,使其成为孤立对象。
从 ARC 的角度来看(如果 PlayerPurpose 只是一个普通的非托管对象),原来的 PlayerPurpose 实例现在没有引用,因此可以从内存中清除它——但是在托管对象上下文中会发生什么? CoreData 是否将其识别为孤立对象并通过上下文将其删除?
如果不是,那么我假设如果要删除对它的所有引用,我必须小心删除通过上下文创建的任何托管对象。假设是这种情况,是否有一个好的模式可以用来确保从 NSManagedObjectContext 中清除孤立对象并且它们不再存储在持久存储中?
谢谢!
【问题讨论】: