views:

518

answers:

1

I have two classes A and B with a many-to-one relationship from A to B (multiple A objects may reference the same B). The question is, if the delete rule on the A side is Cascade, will B be deleted only when the last referencing A is deleted or will it be deleted the first time an associated A is deleted. The delete rule for the B side of the relationship is Nullify if that matters.

Also, I read in the Core Data docs that the Optional flag matters in some cases. But it wasn't clear how the relationships they were illustrating related to my case. They were talking about a containment case (B is owned by A) whereas my case is one of subscription/association (B is related to A).

I could simply manage deletion programmaticaly in the code but wanted to allow Core Data to do the right thing if possible. But it's not clear that the garbage collection semantics that I'm looking for are supported in Core Data.

Any suggestions?

+3  A: 

The model that you describe has B having a to-many relationship with A:

B <-->> A

and the delete rules:

  • B to A: nullify
  • A to B: cascade

In this case, when you delete an A object, that will cascade and delete its B object. Any other A objects with that B object will be updated to reference nil.

gerry3
Thanks for the response. That's what I was afraid of. Changed my code to manage the behaviour manually.
David Goodine
An accept and/or up vote is always appreciated.
gerry3
Sorry, can't up-vote cuz I'm a noob. Will do it when I have 15 points.
David Goodine
Ok, I would appreciate that. Also, you can accept answers.
gerry3