1

I've got a chat app I'm working on, and I don't undertand very well what relationships I should be using. Here's the setup:

                   / Folder 1 \
Account > Mailbox >- Folder 2 - > Message
                   \ Folder 3 /

As you can see, I'll have a lot of messages, but only one of each of the other entities. What relationships should I set up here so that when a Account is deleted, it will delete everything going down the line as well? Of course when a message is deleted, it shouldn't delete the account. Ideas?

Yep
  • 653
  • 5
  • 20
  • See: http://stackoverflow.com/questions/2124022/how-to-properly-cascade-delete-managed-objects-in-core-data – magma Sep 04 '12 at 22:55

1 Answers1

2

In your project's data model, you simply need to set the correct 'Delete Rule' on the relationships that are affected.

In this case, it sounds like you're seeking a "Cascade" delete rule. If you were to select your Mailbox entity, and then select it's relationship to messages, and set the delete rule for that relationship as "Cascade" - that will result in the child relations being deleted when the parent (in this case the mailbox) is deleted.

You should definitely checkout the Core Data Programming Guide, which can give you insight in to what each of the different delete rule settings does exactly (personally I think the names are pretty obvious, but still worth a review).

isaac
  • 4,867
  • 1
  • 21
  • 31
  • How about a relationship going back up, would that be cascade as well? – Yep Sep 04 '12 at 23:32
  • As the relationship is defined in both directions (eg, your parent has a relationship to its children, and the inverse relationship would be from the children to the parent). You can set the rule on either relationship. In this case, you'd set 'Cascade' on the parent's relationship to children, and 'No action' on the child's relationship to it's parent. – isaac Sep 05 '12 at 00:37