Reputation: 32061
Relationships in Core Data just confuse me. I've read and read, but I just don't get it. I guess it doesn't help that I'm usually frustrated when reading. I want to do something really simple:
I have an Entity called Pictures
and an Entity called User
. I want Users to be able to like and tag other people in pictures, so each Picture
entity has two relationships:
Picture Entity:
UsersWhoLikedThePicture (to-many):
Destination: User
Inverse: Picture
UsersWhoAreTaggedInThePicture (to-many):
Destination: User
Inverse: Picture
But this is causing so much mix up in use, that I can't even begin to describe. It's inconsistent. Someone likes a picture causes them to be removed as a tagged user, and like one picture causes your likes from all other pictures to be removed. Ahhh it's such a mess..does my structure look ok? How would I model this?
Upvotes: 0
Views: 584
Reputation: 33428
In addition to Matthias Bauch answer, I could give you some hints to understand relationships.
First, when you deal with Core Data you have to think in term of objects. By means of this astraction you could think at your model as a graph where nodes are the entities that you created in the model, while relationships are the links among those entities.
Now, about relationships, they could be of different types: one-to-one, one-to-many and many-to-many. Based on the type of relationships you have, you can create different link in the objects graph. For example, if a User
has a to-many relationship with Picture
, it means that each instance of object (of type NSManagedObject
) has a link to different Picture
s. User
works as the source, Picture
s, as the destination.
Inverse relationships are used by Core Data to maintain the graph consistency. In particular, they are useful when you deal with delete rules.
Each relationship has a delete rule associated with it. Cascade means that if you delete an object, say the User
, Core Data will delete the object (the Picture
s) linked to it for you. Deny doesn't allow to delete an User
if there are Picture
s linked to it. Nullify means the link from a Picture
to a User
will be broken. It doesn't mean that the objects are deleted. In terms of object graph, it means that you haven't anymore a link between those objects. No Action means that the source is deleted, the destination is always there and it continues to point to an object that doesn't exist anymore. So, unlike Nullify, you need to broke that link manually. If not you could have a graph inconsistency. Try to avoid this type of relationship.
If you want to know something else, let me know.
EDIT
Take a look at Core Data Programming Guide Relationships section for further info.
Upvotes: 3
Reputation: 90117
I don't know if this is even possible, but it sounds like you used the same inverse target for two relationships. Don't do that.
The right way would be something like this:
Upvotes: 3