假设我有一个名为Player
的CoreData实体类型,并且它具有名为PlayerPurpose
的实体类型的一对一关系(purpose
)。为了完整性,假设我们在PlayerPurpose
中有一个反比关系,称为parentPlayer
。请看下面的SWIFT代码:iOS中的孤儿对象CoreData
// 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对象时,它在数据的唯一参考值设为零(或与其他物体代替)托管对象内部?
这与关系删除规则没有什么关系,因为我没有明确删除任何对象 - 我将它从任何有意义的关系中删除,使其成为孤儿。
从ARC的角度来看(如果PlayerPurpose只是一个普通的非托管对象),原始的PlayerPurpose实例现在没有引用,所以它可以从内存中清除 - 但是托管对象上下文会发生什么? CoreData会将此识别为孤立对象并通过上下文将其删除吗?
如果不是,那么我假设我必须小心删除通过上下文创建的任何托管对象,如果我要摆脱对它的所有引用。假设情况是这样,是否有一个好的模式用于确保孤立的对象从NSManagedObjectContext中清除,并且它们不再存储在持久性存储中?
谢谢!
感谢您的回答。它证实了我的怀疑。 – FTLPhysicsGuy