Have a core data question that I am trying to solve for the past couple of hours (5hrs).
I have two entities: Student and Class.
Student attributes: name, grade Class attributes: name
Relationships:
- Student <<------>>Class, both have to-many relationship.
- I create student 1.
- Then create Class 1, Class 2.
- I then add Student 1 to Class 1
- I then add Student 1 to Class 2.
Now when I change the grade of student 1 via the Class 1 its also change the grade for Class 2. So how do I solve this problem. Thanks a bunch.
UPDATE (1): According to answers, I have开发者_StackOverflow modeled the following:
I want to add the same student to different classes: So is the relationship between Student and Class right?
Create a new Class to hold the Grade for a Student and Grade. Something like StudentClassGrade or whatever
I would model it as
Grade:
- attribute: gradeValue (0-100)
- relationship: student
A Class contains a collection of these
- relationship: grades
it is highly suggested all relationships in core data have a reverse relationship, thus:
Grade:
- (reverse) relationship: class
Student:
- (reverse) relationship: grades
Of course, what's "forward" and what's "reverse" it purely semantics.
The complete answer, thanks to all the comments above and also atomicbird #iphonedev irc channel:
To add the same student to different classes and have or edit the student's grade pertaining to each class: the model is:
If I am assuming it right, for each class, student has a grade. If that is the case then you should have student_class and student_class_grade models. student_class ---one to many---student_class_grade.
精彩评论