We have a Listing entity and a ListingAddress entity in a one-to-many relationship. A ListingAddress shouldn’t exist without a Listing. I’d like any ListingAddress that is removed from a Listing’s addresses relationship to be deleted from the context. Is that possible, or do I have to do it by hand?
We have a Listing entity and a ListingAddress entity in a one-to-many relationship. A
Share
You have to do it by hand.
If the listing is deleted, it’s straightforward. See the Relationship Delete Rules. You can set it up to “Cascade” whereby the related object will be automatically deleted. You’ll still of course need to
saveany changes made to the context:But in your case, where the listing isn’t deleted, your orphaned ListingAddress objects have to be cleaned up manually. Here’s a couple of posts that cover that issue:
Deletions in a many-to-many structure
Core Data: re-setting to-many relationship