entropy
entropy

Reputation: 3144

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? Will the other end be deleted? Or will this result in an error?

Upvotes: 0

Views: 447

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70966

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. 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. This is separate from setting the relationship to 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.

Upvotes: 2

Related Questions