Question

I have simple core data model. I have student, who has parents - NSSet. When I have this code:

if(!student.parents.count == 0){
    Parent *mother = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:cdh.context];
    [student addParentsObject:mother];
}

How can I get later the object "mother" from the NSSet (studen.parents)? Thank you.

How can I edit the object mother? For example mother has attribute name (mother.name). How can I edit the attribute the simplest? I think by student, not only the object mother. For example I know, that I want to edit mother from student, who has ID "XXX". How can I do it?

Was it helpful?

Solution

The problem (if it is a problem) is that you have done nothing to indicate that a particular parent is the "mother". There is no "object mother" in the set of parents. The word "mother" is not a "name" in any meaningful sense whatever.

When you added the parent in the code above, you used a variable named "mother", but that is just a temporary reference; it does not designate anything about the object to which this variable points, the object that is now added as a Parent (and the variable vanishes in a puff of smoke two lines later in any case). It would not make the slightest difference if the variable name had been scrumdiddlyumptious:

Parent * scrumdiddlyumptious = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:cdh.context];
[student addParentsObject: scrumdiddlyumptious];

The effect would be exactly the same as in your code.

Ultimately, this is an issue with your model. Your model is faulty in relation to your future desires. If knowing that a particular parent is a "mother" is important to you, it is up to you to have an attribute in the Parent entity which you can set to indicate that. Now you can fetch based on that attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top