在NSFetchResultController中设置NSSortDescriptor键的问题 [英] Issue with setting NSSortDescriptor key in NSFetchResultController
问题描述
到上面的关系图,以前,我有一个太多的关系之间的Assignment和问题实体,这是打破修复一个问题排序问题。
现在,与Assignment to Question的关系是通过连接表AssignmentQuestion。
这里的想法是根据AssignmentQuestion表的questionOrderIndex属性排序问题。
通过NSFetchResultController的赋值问题。我写了一个谓词来提取问题。但是由于我想要问题排序基于questionOrderIndex,我的排序描述符通过问题实体的排序键是assignmnetQuestion.questionOderIndex。
但是使用这个排序键,我获取异常:Exception = to-many key not allowed here。经调查,我发现这是因为一对多的关系从问题到AssignmentQuestion表。
我的问题是:
1)如何基于questionOrderIndex为Sort描述符设置排序键?
2)我们真的需要一个连接表
<$> p $ p>
NSString * predicateString = [NSString stringWithFormat:@self.evaluationQuestion.assignmentEvaluation.assignmentId ==%@,@1];
如果您不需要监听问题(在您的视图控制器外部),您可以获取AssignmentQuestion实体,然后预取附加的问题。
稍后在您的视图控制器中,而不是使用 AssignmentQuestion
实体,使用其预取的问题
关系
// code not tested
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@AssignmentQuestion];
[request setPredicate:[NSPredicate predicateWithFormat:@assignmentEvaluation.assignmentId ==%@,assignmentId]];
[request setRelationshipKeyPathsForPrefetching:@ [@question]];
[request setSortDescriptors:@ [[NSSortDescriptor sortDescriptorWithKey:@questionOrderIndexascending:YES]]];
也查看此处
With respect to the above relationship diagram, Earlier, I had a too many relationship between Assignment and Question entity and this is broken to fix a question ordering issue.
Now the relationship to Assignment to Question is through a join table AssignmentQuestion.
Idea here was to sort the question based on the questionOrderIndex property of the AssignmentQuestion table.
I am fetching the assignment questions through a NSFetchResultController. I have written a predicate to fetch the questions accordingly. But since I want the questions to be sorted based questionOrderIndex, my sort key for the sort descriptor through question entity is assignmnetQuestion.questionOderIndex.
But with this sort key, I am getting an exception: "Exception = to-many key not allowed here". Upon investigation, I found that it is because of "one to many" relation from Question to AssignmentQuestion table.
My questions are:
1) How to set the sort key for a Sort descriptor based on questionOrderIndex?
2) Do we really need to have a join table here in core data to sort the questions in order?
Predicate to fetch Questions:
NSString *predicateString = [NSString stringWithFormat:@"self.evaluationQuestion.assignmentEvaluation.assignmentId == %@", @"1"];
If you don't need to listen for changes on the questions (external to your view controller), you could fetch the the AssignmentQuestion entities instead, and prefetch the attached questions.
later, in your view controller, instead of using the AssignmentQuestion
entities, use their prefetched question
relationship
//code not tested
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"AssignmentQuestion"];
[request setPredicate:[NSPredicate predicateWithFormat:@"assignmentEvaluation.assignmentId == %@",assignmentId]];
[request setRelationshipKeyPathsForPrefetching:@[@"question"]];
[request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"questionOrderIndex" ascending:YES]]];
also see here
这篇关于在NSFetchResultController中设置NSSortDescriptor键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!