Search code examples
ioscore-dataentity-relationship

Can a Core Data relationship be its own inverse relationship?


Can a Core Data relationship be its own inverse relationship?

I have a generalized entity that is used to represent different types of objects. Some of these objects have to-many relationships to each other. Do I simply set the relationship as its own inverse, or create a new relationship with the same destination as the inverse?

Since Xcode gives you the option to set a relationship as its own inverse, I'm assuming it's okay, but would just like to make sure. Thanks in advance.


Solution

  • Yes there is no problem with this kind of relationship. You may always have links to objects in the class, even to the object itself.

    Just in case you have to delete your objects from ManagedObjectContext you need to consider the deletion rules here https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW1.

    But really no problem in general with your approach...