简体   繁体   English

在核心数据中,如果从另一端将非可选关系设置为nil会发生什么?

[英]In core data what happens if you set a non-optional relationship to nil from the other end?

I have a one-to-one relationship that is optional on one end and non-optional on the other, what happens if I set it to nil from the optional side? 我有一对一的关系,一端是可选的,另一端是非可选的,如果我从可选端设置为nil会发生什么? Will the other end be deleted? 另一端会被删除吗? Or will this result in an error? 或者这会导致错误吗?

If you have a one-to-one relationship, and you set it to nil from one side, it is set to nil on the other side as well. 如果你有一对一的关系,并且你从一边设置为nil,那么另一边也设置为nil。 That's a fundamental part of how inverse relationships work. 这是反向关系如何运作的基本部分。

Nothing gets deleted unless you delete it. 除非您删除它,否则不会删除任何内容。 If you delete an object with a one-to-one relationship to another object and the delete rule for the relationship is set to "cascade", then the related object will also be deleted. 如果删除与另一个对象具有一对一关系的对象, 并且该关系的删除规则设置为“cascade”,则相关对象也将被删除。 This is separate from setting the relationship to nil. 这与将关系设置为nil分开。

Will this result in an error? 这会导致错误吗? If the relationship is not optional, you'll get an error if you save changes while the relationship is nil. 如果关系不是可选的,如果在关系为零时保存更改,则会出现错误。 It's allowed to be nil in memory; 它被允许在记忆中为零; checking the optional flag happens when saving changes. 保存更改时,会检查可选标志。 But before then, you could reassign the relationship to something non-nil, or delete the object, and not get an error. 但在此之前,您可以将关系重新分配给非零值,或者删除对象,而不是出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM