How do I model relative scores between entities in CoreData
I am new to CoreData and am struggling to work out the correct way to model a particular relationship. I have an entity called 'Friend' with a few attributes such as 'name', 'age', 'sex' etc.
I would like to be able to model a score between two instances of Friend and am having trouble getting my head around the best way to do this.
For example for 3 friends called A, B and C, there may be scores like so:
A <-> B: 3
A <-> C: 2
B <-> C: 4
or in matrix form:
A B C
A 0 3 2
B 3 0 4
C 2 4 0
The best I have come up with is to have a 'Score' entity with a 'value' integer attribute and two relationships to 'friendA' and 'friendB' - but if this 开发者_如何学运维is the correct approach how should I model the inverse relationships on the Friend entity?
Many thanks in advance for any help!
Your idea of a Score entity is the best I can think of without more detail about the entire design. However the inverse relationship is simple enough.
Create a many-to-many relationship between Friend and Score.
On the Score side you set the min and max count to 2.
Set the Friend side to no min and no max.
This is assuming that the order of the friends in relation to the score is not important.
You could create inverses using your approach by having two to-many relationships from Friend to Score (maybe called "scoresA" and "scoresB").
This is a little different than what Marcus suggests since you would have to combine both sets to get all the Score objects for a particular Friend (or check both sets if you wanted to see if a particular Score object was associated with a particular Friend).
The approach you take should be whatever is easier for you to work with in your application. However, Marcus may be able to provide reasons why his approach is better since he literally wrote the book on Core Data.
I'm working on a similar project, and I have been thinking a fun way to implement it would be for there to be a "game" entity, which has a to-one relationship to a winner.
What I have yet to learn is how to display the count of wins against a given friend.
精彩评论