为核心数据中的每个多对多实体关系设置新值
这让我发疯,而且很可能我做错了。
所以我有两个实体:具有多对多关系的条件和选项。
每个属性都
Option: optionName Criteria: criteriaName criteriaRank
在我的应用程序中,我创建所有选项并设置其名称。
然后我插入一个条件并将其添加到每个选项中:
Criteria *aCriteria = [NSEntityDescription insertNewObjectForEntityForName:@"Criteria" inManagedObjectContext:[decision managedObjectContext]];
aCriteria.criteriaRank = [NSNumber numberWithInt:1];
// Add the new criteria to the criteria array and to the table view.
[criteriasArray addObject:aCriteria];
NSEnumerator *enumerator = [fetchedOptions objectEnumerator];
Option *anOption;
while(anOption = [enumerator nextObject])
{
[anOption addCriteriasObject:aCriteria];
}
我想要的是,对于每个选项,我可以对每个条件进行排名,但是当我加载每个选项的条件列表并通过滑块设置其排名并保存时,所有其他选项显示我刚刚更新的相同值。
我通过以下方式获取条件:
//fetch all criterias
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"criteriaName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSMutableArray *sortedCriterias = [[NSMutableArray alloc] initWithArray:[option.criterias allObjects]];
[sortedCriterias sortUsingDescriptors:sortDescriptors];
self.criteriasRankingArray = sortedCriterias;
我希望能够添加一个条件,然后在每个选项下对每个条件进行排名。 我是否以错误的方式处理这个问题?帮助!!
谢谢。
This is driving me crazy and most likely I am doing this wrong.
So I have two entities: Criteria and Option that has a many to many relationship.
The attributes for each are
Option: optionName Criteria: criteriaName criteriaRank
In my app, I create all my options and set its name.
Then I insert a criteria and add that to each option :
Criteria *aCriteria = [NSEntityDescription insertNewObjectForEntityForName:@"Criteria" inManagedObjectContext:[decision managedObjectContext]];
aCriteria.criteriaRank = [NSNumber numberWithInt:1];
// Add the new criteria to the criteria array and to the table view.
[criteriasArray addObject:aCriteria];
NSEnumerator *enumerator = [fetchedOptions objectEnumerator];
Option *anOption;
while(anOption = [enumerator nextObject])
{
[anOption addCriteriasObject:aCriteria];
}
What I want is that for each option, I can rank each criteria but when I load the criteria list for each option and set its rank via a slider and save, all the other options display the same value i just updated.
I am fetching the criteria in the following manner:
//fetch all criterias
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"criteriaName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSMutableArray *sortedCriterias = [[NSMutableArray alloc] initWithArray:[option.criterias allObjects]];
[sortedCriterias sortUsingDescriptors:sortDescriptors];
self.criteriasRankingArray = sortedCriterias;
I want to be able to add a criteria and then under each option, rank each criteria.
Am I approaching this the wrong way? Help!!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将相同的 Criteria 对象添加到不同的选项,那么您将看到此行为。您必须为每个 Option 对象创建全新的 Criteria 对象。看起来您正在跨多个选项重用 Criteria 对象,这听起来不像您想要的。您似乎希望每个选项对象都有唯一的条件。
If you are adding the same Criteria objects to different Options, then you will see this behavior. You'll have to create brand new Criteria objects for each of your Option objects. It looks like you are reusing your Criteria objects across multiple Options, which does not sound like what you want. You seem to want unique Criteria for each Option object.