Search code examples
core-dataentitynsmanagedobjectdatamodel

Having an NSManagedObject property of type of another NSManagedObject


I looked over the SO, but strangely didn't find similar question. So the question is: If i have an entity called A and an entity called B, can B object have a property of type A? NOT relationship, but a property. I surely can use relationship as a property, but in my case i need an A object to have a property with type of A object. for example Entity called Human. And a property called child which is a Human too.

Human* parent=[Nsentity....bla bla
Human* child=parent.child;

Is there a way to do this not using transformable properties? I tried non-inverse relationship to self, but it rises a warning, and im like afraid that its gonna be a mess after all. cos in my case "parent" and "child" might be exactly the same object. so parent.child might be equal to parent


Solution

  • The is no prevention for "self" relationships.

    An entity A may have a relation (one-to-one or one-to-many) with entity A or any of its descendants (or any other entity for that matter).

    Define A like so (for one-to-one relationship):
    parent (reltionship with inverse A->child)
    child (relationship with inverse A->parent)

    You probably don't want this to be a property as CoreData will not maintain it as part of the object graph (cascade rules and such).