views:

52

answers:

3

Hi,

I am using CoreData but I don't think I got the inverse relationship concept right. Could any one explain what is it? Let's say I have a "File" entity with "files" relationship and "parentFile" relationship

"File" entity has:

a to-many relationship "files" with itself ("File").

a to-one relationship "parentFile" with itself ("File").

What would the the inverses for this two relationships? Thanks in advance.

Ignacio

+4  A: 

When you have two relationships with different entities that each point to each other, the "Inverse" field is how you tell XCode that those two relationships "go together". If I run a car rental business, I might have an entity for my cars with a foreign key indicating which lot the car is stored at. The lots entity then, would naturally have a to-many relationship, which in a relational database would just be accomplished with the same join as the car's to-one relationship -- no extra key needed. In Core Data, we (users) don't really deal in keys, as such, so Destination, Inverse, and To-Many are the inputs that Core Data needs from you to implement the relationships. Further, that information helps Core Data figure out what to do when you delete objects on one side or the other of the relationship.

In your example, if I'm reading it right, those two relationships are the inverse relationships of each other.

Seamus Campbell
A: 

You can tell you have an inverse relationship in the model view in Xcode visually when you have a line connecting two entities and both ends have arrows on them.

David Weiss
+3  A: 

To put it plainly:

The Inverse of the to-many relationship files is the to-one relationship parentFile.

And visa versa … so …

The Inverse of the to-one relationship parentFile is the to-many relationship files.

Joshua