If I have two entities; Foo and Bar. And Foo has two properties; bar1 and bar2 of type Bar. Now does Foo have a one-to-many releationship to Bar? At least should it be modeled as that in Core Data? Or is it two one-to-one releationship? How do I set that up properly in Core Data with inverse? The one-to-many releationship I understand but not the last releationship type? Is that even possible or good way to that?
preguntado el 22 de mayo de 12 a las 16:05
And Foo has two properties; bar1 and bar2 of type Bar. Now does Foo have a one-to-many releationship to Bar?
No, Foo has two relationships with Bar, and those relationships are named
bar2. Whether they're "to-one" or "to-many" depends on how you specified them. Select each in the model and see whether the "to-many" checkbox is checked.
At least should it be modeled as that in Core Data?
That depends on what you're trying to model. Do you want to have two separate relationships to specific object (that'd be two different "to-one" relationships), or do you want a single relationship to a group of objects (one "to-many" relationships)? Here's an example that might help clarify things...
A business may have zero or more employees. A business also has a single chief executive officer and a single chairman of the board. So the Business entity might have a "to-many" relationship named
employees with the Person entity. It might also have "to-one" relationships named
chairman, again with Person. Those might not be strictly necessary -- maybe the CEO and Chairman are both employees, so you could find them by searching the set of employees and filtering by job title. But it can be handy to have them as separate relationships if you're going to use them often and don't want to have to search through thousands of other employees every time you do.
How do I set that up properly in Core Data with inverse?
Again, it'll depend on what you're modeling. For example, the inverse of the
employees relationship would be Person's
employer "to-one" relationship.
Entities have several different kind of properties, they have attributes and relationships (and fetched properties, but that's beside the point). It sounds like Foo and Bar are two classes you created with Foo having Bar properties. Then you want assimilate those as Entities in a Core Data model.
Typically, things are done the other way around. You create Entities in the Core Data model editor. You then link the entities by creating relationships. If you add a relationship from Foo to Bar, then it is good practice to create an inverse relationship from Bar to Foo.
- Create the relationship from Foo to Bar
- Create the relationship from Bar to Foo
- Select that last relationship and set its inverse to the first one
Now if you want Xcode to generate the matching NSManagedObject subclasses Foo and Bar, then you can select the Entities and in the Xcode Editor menu, select "Create NSManagedObject subclasses"
You'll then see you're two classes with the proper attributes to match how your model is set up.
I've authored a book on Datos principales that explains all this is much deeper details.
This is two one-to-one releationship. To make inverse just select you entity in inverse field (When you create relation).