Confusión de relación de datos básicos

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

3 Respuestas

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 bar1 y 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 ceo y 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.

contestado el 22 de mayo de 12 a las 17:05

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.

  1. Create the relationship from Foo to Bar
  2. Create the relationship from Bar to Foo
  3. 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.

contestado el 22 de mayo de 12 a las 17:05

You should clarify that first paragraph, because it's misleading. Entities definitely have properties, and those properties come in two types: attributes and relationships. I'm sure you're aware of this, having written a book about it, but that first line in particular probably won't lessen the OP's confusion. - Caleb

Totally right. I got fixed on calling attributes properties. Thanks for calling it out. - privado

One of the great problems in our field is the shortage of generic terms: object, widget, attribute, property, member, field, value... Figuring out how these things relate to each other is one of the major activities involved in learning a new language or framework. - Caleb

This is two one-to-one releationship. To make inverse just select you entity in inverse field (When you create relation).

contestado el 22 de mayo de 12 a las 16:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.