Reputation: 147
I am trying to set a relationship between inputted core data values. I currently have it set up so when I add the value it creates the relationship in the corresponding entity which I can view in a detail view.
What I am trying to achieve is to add the relationship to a existing value within the entity that is held in a string RoutineText
. So instead of creating a second identical entry the relationship is added to the new entry. So in the detail view both entries will be viewable.
The current situation when inputing the values
So instead of it creating:
TestName1 ----> TestName1Detail
TestName1 ----> TestName2Detail
it would create:
TestName1 ----> TestName1Detail + TestName2Detail
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new device
ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
Routines *routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Routines"inManagedObjectContext:context];
RoutinesDetails *routineEntityDetail = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
//Create Relationship
[routineEntity addRoutinedetObject:routineEntityDetail];
//Add attribute values
//[routineEntity setValue: RoutineText forKey:@"routinename"];
[routineEntityDetail setValue: info.name forKey:@"image"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
I hope that is clear.
Upvotes: 0
Views: 655
Reputation: 73936
Of course it's acting this way - you're creating a new Routines
object:
Routines *routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Routines"inManagedObjectContext:context];
If you want to associate a new RoutinesDetails
object with an existing Routines
object, you don't create a new Routines
object, you use the one you already have.
Given that the preceding comment explicitly states a new object is being created, and that it refers to a completely different object, I'm guessing you've copied and pasted this code instead of writing it. I suggest going through the tutorials instead of trying to make somebody else's code work without understanding what is going on.
Upvotes: 1