views:

104

answers:

1

I am using coredata framework. In my NSManagedObjectModel i am using three entities that are Class, Student and Score where class and student have one-to-many & inverse relationship and Student and Score have also inverse but one-one relationship.

Score entity has all optional attributes and having default '0' decimalVaue, which is not assigned at the time new Student is added. But later i want to assign them score individually and also to particular attribute not all of score attributes in a go. I am able to create and add Students to particular Class but dont have any idea how to call particular student and assign them score. For example I want to assign Score'attribute "dribbling" [there are many attributes like "tackling"] a decimal value to Student "David" of Class "Soccer" ,how i can do that?

Thanks in advance for any suggestion.

+1  A: 

I have found a solution. First time when creating or adding a new student to class (NSManaged)object in class (NSManaged)Context, also add a new instance of Score (NSManaged)object having newly created or added student in its relationship and at the same time assign them default '0' value to all other attributes. Later you can reassign values depending upon situation.

[student setValue:@"Beckham" forKey:@"lastName"];
[student setValue:@"Soccer" forKey:@"belongsToClass"]; 

after this new student has been added keep a reference to it and use it in new Score object as-

Score *score = [NSEntityDescription insertNewObjectForEntityForName:@"Score" inManagedObjectContext:managedObjectContext];
score setValue:[NSNumber numberWithInteger:0] forKey:@"dribbling"];//attribute
[associatedScore setValue:[self newlyAddedStudent] forKey:@"belogsToStudent"];//relation

Thats it.

Kundan Pandit