核心数据关系 – 添加到现有值

我正在设置input的核心数据值之间的关系。 我现在已经build立了,所以当我添加它创build关系的价值,在我可以查看详细视图相应的实体。

我想要实现的是将关系添加到stringRoutineText的实体内的现有值。 因此,而不是创build第二个相同的条目关系被添加到新条目。 所以在详细视图中这两个条目将是可见的。

input值的当前情况 输入值的当前情况

所以,而不是创build:

TestName1 —-> TestName1Detail

TestName1 —-> TestName2Detail

它会创build:

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]); } 

我希望这是明确的。 在这里输入图像说明

当然,这是这样的 – 你正在创build一个新的Routines对象:

 Routines *routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Routines"inManagedObjectContext:context]; 

如果要将新的RoutinesDetails对象与现有的Routines对象相关联,则不要创build新的Routines对象,而是使用已有的Routines对象。

鉴于前面的评论明确指出一个新的对象正在创build,并且它指向一个完全不同的对象,我猜你已经复制和粘贴这个代码,而不是写它。 我build议阅读教程,而不是试图让别人的代码工作,不知道发生了什么。