核心数据关系 - 添加到现有值 [英] Core Data Relationship - Add to existing value
问题描述
我试图设置输入的核心数据值之间的关系。我目前有它的设置,所以当我添加的值,它创建的关系在相应的实体,我可以在细节视图中查看。
我想实现的是将关系添加到在字符串中保存的实体中的现有值 RoutineText
。因此,不是创建第二个相同的条目,而是将关系添加到新条目。因此,在详细视图中,两个条目都可以查看。
输入值时的当前情况
因此,不是创建: p>
TestName1 ----> TestName1Detail
strong> TestName1 ----> TestName2Detail
会创建:
TestName1 ----> TestName1Detail + TestName2Detail
NSManagedObjectContext * context = [self managedObjectContext];
//创建新设备
ExcerciseInfo * info = [_fetchedResultsController objectAtIndexPath:indexPath];
Routines * routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@RoutinesinManagedObjectContext:context];
RoutinesDetails * routineEntityDetail = [NSEntityDescription insertNewObjectForEntityForName:@RoutinesDetailsinManagedObjectContext:context];
//创建关系
[routineEntity addRoutinedetObject:routineEntityDetail];
//添加属性值
// [routineEntity setValue:RoutineText forKey:@routinename];
[routineEntityDetail setValue:info.name forKey:@image];
NSError * error = nil;
//将对象保存到持久存储
if(![context save:& error]){
NSLog(@无法保存!%@%@ ,error,[error localizedDescription]);
}
我希望很清楚。
当然是这样的 - 你创建了一个新的 Routines
对象:
Routines * routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@RoutinesinManagedObjectContext:context];
如果要关联新的 RoutinesDetails
对象与一个现有的 Routines
对象,你不创建一个新的 Routines
对象,你使用你已经有。
由于前面的注释显式声明一个新对象正在被创建,并且它引用一个完全不同的对象,我猜你已经复制和粘贴这个代码而不是写它。我建议通过教程,而不是试图让别人的代码工作,而不了解发生了什么。
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.
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.
这篇关于核心数据关系 - 添加到现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!