Core Data saving array and dictionaries - best practices - ios

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?
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?

You should use core data with one to many relationship. This 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
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.
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.

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

Related

How to design neo4j db nodes that have ordered relationship with other nodes

I'm trying to design a Neo4j graph database, and I will illustrate my specific requirement. I'm designing an app that allows users to collaborate on the books and magazines they have read. Requirements:
The same Book can be read by multiple people
The order in which a specific person reads the books is important. For example, I want to be able to represent that Person A read Books B1 and B2 in that order, while Person B may have read Books B3, B2 and B1 in that order.
I'm thinking of having nodes representing a Book, Magazine, Person, etc.
What is the best way to ensure the order/sequence information? A couple of options I thought about:
Store a order ID or timestamp in the relationship between a Person and a Book node and use that to query all Books read by the person in the right order.
Store a Next/Previous relationship between Book nodes, but this approach will not work because the order can vary depending on which person read the books.
To me this is the way to go, easy and effective.
Store a order ID or timestamp in the relationship between a Person and
a Book node and use that to query all Books read by the person in the
right order.
Storing a Next/Previous relationship will not work, because you would have to save UserID in the NEXT relationship between books and that is just nonsense.

What is a many to many relationship?

I'm a bit confused on what a many to many relationship is. I'm wondering if the following is a many to many relationship:
A student at a school has many clubs. A club at a school has many students. Let's say that the student has many attributes: firstname, lastname, phone, age, email, etc. A club only has one attribute: a name.
When I make a new club, I want to be able to give the club a name and one or more students. Upon making the club, I want that club to be associated with those students and those students to be associated with that club.
When I make a new student, I want to be able to give the student a firstname, last name, etc, and one or more clubs. Upon making the student, I want that student to be associated with those clubs and those clubs to be associated with that student.
I also want to display a club's students and a student's clubs on their show pages.
I've read that a many to many relationship is when you have a join table that lets you access common attributes of the resulting students and clubs, but there are no common attributes in my case.
Do I have a many to many relationship here? If so, do I use a HABTM or has_many, through relationship?
Actually yes you DO have common attributes.
You stated yourself that a Student has many Clubs
And a Club has many Students.
What is in common? Students and Clubs.
What now follows is to define what a Student and a Club actually are, which you already did.
A Student is a combination of firstname, last name, etc... What you have not specified is what makes a Student UNIQUE. A club also must be defined as to what will make it UNIQUE. While for academic purposes, you could say the name is what makes it unique, in real live, that would probably not be the best solution.
Usually for performance purposes, each student is given a unique Autoincrement ID (which is a number).
Same thing can be done with the Club.
You create a 3rd table which is what creates the Many to Many relation.
In that 3rd table, you have 2 columns. One with the Unique Index for the Student, and the other column with the Unique Index for the Club. You simply add an entry on that table in which you wish to relate a student to a club.
Since you can have many students assigned to the same club, and you can have many clubs assigned to the same student, you have a many to many relation.
Edit: As mentioned in another answer, your 3rd table should also declare the combined indexes as unique, so that you don't add the same entry multiple times.
You have a many to many
Create an id for each table that is unique for that table typically an auto incrementing int.
Then a third table that is a junction/intersect table call it X.
Put a row in X with the student id and club id if the student has the club and vice versa. It would have a unique composite key in table X across both id's in it.
The composite would guarantee no duplicate rows in X
Yes indeed there is a many-to-many relationship here, use HABTM. Also, why do you say that there are no attributes in common? Club names and student names are definitely common attributes in this case.

Fetch entities with 0 junction tables

We have a few different entities. To explain a little better, here is example structure:
We have a lot of students.
We have a lot of homeworks.
Each homework has N (varies per homework) tasks.
There is a junction table connecting students and tasks.
We want to assign some tasks to certain students in a homework. Let's say one homework has 5 possible tasks, we want each student to get one or more tasks.
At the moment, interface lists all students with some properties (for this case let's say - average grade, hair colour, name, gender etc.) and has 5 checkboxes (there are 5 tasks in selected homework). We can use filters to show only students with average grade of 4, or just female students etc.
After a while, you would assign 98% of students, but you notice there are 2% students without any tasks for selected homework (some statistics shows you that). Instead of going through all few thousand students, we'd like to create filter which would use existing filters AND apply additional filter which would show all students which have 0 tasks where task.homeworkId = X
Right now, in my head there is a possible solution, but I'm not sure if this is possible using breeze:
from students
where (OLD_FILTERS)
and ((from junction_table
where task.homeworkId = selectedHomeworkId
and student.id = $parent_query.id).count() = 0)
I've been over this for a while now, can't come up with a nice clean solution. Only thing comming to my mind is pretty complex solution with manual filtering of all student properties and this new condition on server.
Thanks in advance for any help.
EDIT
Tables are related as follows:
student - junction table = 1 to many
task - junction table = 1 to many (basically, it's many-to-many student-task through junction entity)
task - homework = many to 1 (many tasks per one homework)
some perception on the model:
Student:
Id
Property1
Property2
...
Homework
Id
Property1...
Task
Id
HomeworkId // this is foreign key
Property....
JunctionTable
Id
TaskId // foreign key
StudentId // foreign key
Thanks to DenisK for help. We used older version of Breeze without the needed option. Solution is simple predicate:
var pred = breeze.Predicate.create("studentTasks", "any", "task.homeworkId", "==", homeworkId).not();

iOS - Many-to-Many data confusion?

I'm developing a simple iOS application with the following database setup. There is an Athlete entity which has a many to many relationship with Workout. Workout has a to many relationship with Workout Scores. Athlete<<->>Workout<->>Workout_Scores. I think i've set up my model incorrectly though. I was thinking athletes can share workouts (e.g. multiple athletes have the same workout object), or, an exercise may be exclusive to one person. However, the exercise score is strictly for one athlete, not shared. You can have up to 1 score for each workout. 2 athletes can have the same workout, but their score should be separate. Did I set up my model correctly? Should the score entity be related to athlete, not workout?
It sounds like you want to use Workout_Scores as a sort of join table (although that of terminology isn't appropriate for an object-graph framework like CoreData). Your Workout_Scores entity should have two to-one relationships to Athlete and Workout. Athlete should have a to-many relationship to Workout_Score (an athlete may have many workout scores), and Workout should have a to-many relationship to WorkoutScore (a workout may have many workout scores that originated from a single or from many different athletes).
Here's what I propose for your data model:

can a relationship in core data link an instance of an entity twice to another instance

I want to make a to-many relationship that can save an instance of an entity more than ones.
for exemple, lets say I have two entities: buyer, product.
now I have a buyer "jon", and a product "tomato", jon should be able to have more then 1 tomato.
another solution may be to save a counter somewhere, but i cant find an efficient way to do so.
Two ways to do that, depending on your needs:
Create a different object for each individual tomato, so that you're modeling every tomato anyone ever purchases. This probably isn't a great strategy for tomatoes, where you don't care much about the individual objects, but it could be a good plan for cars, where each vehicle is unique and should be tracked separately.
Create an entity that represents a purchase transaction. Jon isn't buying the same tomato several times, he's buying some tomato several times. The thing you want to track isn't the tomato but the purchase.

Resources