I am new to the Core Data. I am trying to create an employee database using Core Data. I store the user name and his birthday in an entity Employee
and the employee can have multiple phone numbers. So I have created another entity PhoneNumbers
to store the phone number. I have made the relationship for the two entities. But when I try to insert two phone number for the employee, only the second phone number have the relationship to the employee.
I don't know how to alter my core data model.
This is how I insert the data's to the Core Data.
NSManagedObject *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_managedObjectContext];
[entry setValue:@"Suresh" forKey:@"name"];
[entry setValue:[NSDate date] forKey:@"birthdate"];
NSManagedObject *phoneEntry = [NSEntityDescription insertNewObjectForEntityForName:@"PhoneNumbers" inManagedObjectContext:_managedObjectContext];
[phoneEntry setValue:@"9600492944" forKey:@"phone"];
[phoneEntry setValue:entry forKey:@"owner"];
[entry setValue:phoneEntry forKey:@"phone"];
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"hi %@", [error localizedDescription]);
}
NSManagedObject *phoneEntry1 = [NSEntityDescription insertNewObjectForEntityForName:@"PhoneNumbers" inManagedObjectContext:_managedObjectContext];
[phoneEntry1 setValue:@"1234567890" forKey:@"phone"];
[phoneEntry1 setValue:entry forKey:@"owner"];
[entry setValue:phoneEntry1 forKey:@"phone"];
if (![_managedObjectContext save:&error]) {
NSLog(@"hi %@", [error localizedDescription]);
}