0

Below is an illustration of the kind of data I want to save in Core data. Every city has many schools , every school has many grades and every grade has many students and their details.

I have read a couple of things about Core data and have got it up and starting. But I'm not able to understand how to save an array in core data and is it a good way to do that in the similar case of the illustration?

If i want to save for a particular school an array of total students for that particular grade, would it be a good practice? If yes, is the method provided in this link good to follow?

enter image description here

EDIT : All cities, all schools and all students have same attributes. Whereas each grade has different attributes. So if there are data for 10 grades, there may be 10 types of array for grades.

Also, what if i have a one to many relation between school and students? IE depending on my login i decide whether i need to save school and grades or school and students. How would the relationship be now?

Sharanya K M
  • 1,805
  • 4
  • 23
  • 44

2 Answers2

1

Dont do it the way shown in that link. Create core data entities for each of them (city,school,grade,student). Add relationship between those entities (Eg: City ->> school which means one to many relationships). Check this link http://www.raywenderlich.com/14742/core-data-on-ios-5-tutorial-how-to-work-with-relations-and-predicates. Refer apple document https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/articles/cdRelationships.html as well. Take your time with core data modelling. Hope it helps

sanjaymathad
  • 232
  • 1
  • 5
1

You should use core data with one to many relationship. Thisenter image description here would be your entity structure.

UPDATE: In case you have several grades with different attributes, you can define another entity "GradeType", which contains details of each grades like this:

UPDATE 2:

Let me write down considerations in this scenario.
1. A city can have multiple schools in it, but a school can be only in one city (Branches will have different address ;) ).
2. A school may offer multiple subjects. same subject can be taught in multiple cities.
3. A school may contain multiple students while a student can be enrolled only in one school.
4. A student can register for multiple subjects, while same subject can be registered by multiple students.
5. There can be multiple grades possible for a subject.(lets say 4: A, B, C & D). Similarly, many subjects will follow the same grading system.(A in history, B in Geology etc).
6. A student can have multiple grades. However, the number of grades will be equal to number of subject he/she opted for.

Based on above consideration, this would be your dataModel.image

Here Grades Entity will have entries like this:
grade A for physics is scored by these students.
grade A for biology is scored by these students.

grade B for physics is scored by these students.
grade B for biology is scored by these students.
… … N So on

Let me know if more info needed.

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • But in this structure, there's no segregation for grade 1 2 , 3 etc... what if grade 1 has different attributes, grade two has different attributes, etc? Please do check the edit – Sharanya K M Feb 03 '14 at 11:55
  • this is really helping me.. Just one more thing.. If i need say 5 grade types each having different attributes.. then i would have to use 5 diff entities as type 1 type2 etc? – Sharanya K M Feb 04 '14 at 05:21
  • There's another thing i would like to know. Please check the edit. Thanks for helping me dissect core data... – Sharanya K M Feb 04 '14 at 05:48
  • @SharanyaKM.. If you have 5 gradeTypes, you don't need 5 entities. you need 5 entries/rows in GradeType entity.. – Prince Agrawal Feb 04 '14 at 05:50
  • 1
    @SharanyaKM.. for your updated question, you can have both in single model. you can have one to many relation b/w grades & student as well as school & student in same model. There is no issue – Prince Agrawal Feb 04 '14 at 05:52
  • i understand.. each gradeType has different attributes and not the same.. Say for example subjects.. Each grade has different subjects.. and the score of each student i have to save for that subject – Sharanya K M Feb 04 '14 at 05:55
  • All i want to know is in the above case can i save subjects and their scores as an array for each grade for every student? – Sharanya K M Feb 04 '14 at 05:57
  • You have got it right except for another observation that you have missed the relationship between school and grades. it can be school-->grades-->students or school --> students... But nevertheless this is enough for my purpose to carry on further.. Thanks a lot for your patience and help.. :-) – Sharanya K M Feb 04 '14 at 06:58