Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data one-to-many relation saving entity confusion
    text
    copied!<p>I am working on a project where I am using core data, but bit confused how to use it corectly.</p> <p>I have two entities students and college</p> <p>students has one-to-one relationship to college. That is every student will have one college. college has one-to-many relationship to student. that is college can have lot's of students.</p> <p>However, in my app I am letting the user to add the student first without any college info. So I am keeping the relationship to college optional. In a 2nd view, I let the user update the college information of a student.</p> <p>Now I am while adding a new student I am just updating the basic field related to student (firs name, last name, age, etc.). Now in 2nd view while updating college info I am not sure what is the correct way to do it considering the relationship.</p> <p>The first view pass the student object to 2nd view and I am updating it as follows:</p> <pre><code>College* college = (College*)[NSEntityDescription insertNewObjectForEntityForName:@"College" inManagedObjectContext:self.managedObjectContext]; college.name = @"name" college.address = @"address" [college addStudentObject:self.student]; </code></pre> <p>Am I doing it correctly? what should be the correct way:</p> <ol> <li><p>Adding the college object in 2nd view and connecting the entity by adding the student object in the student relation of a college?</p></li> <li><p>Retrieving the student object first then getting the empty college object from the college relation. Then updating the college object with data and then save the context.</p></li> </ol> <p>I am not sure if the 2nd option make any sense but in case of first option, if I add a new college, the new college get connected with the student but the old college remain in the database as redundant data. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload