iphone - 在 NSFetchedResultsController 中设置 NSSortDescriptor 键的问题

标签 iphone ios ipad core-data nsfetchedresultscontroller

enter image description here

关于上面的关系图,早些时候,我在 Assignment 和 Question 实体之间有太多关系,为了解决问题顺序问题,这被打破了。

现在 Assignment 到 Question 的关系是通过连接表 AssignmentQuestion 实现的。

这里的想法是根据 AssignmentQuestion 表的 questionOrderIndex 属性对问题进行排序。

我正在通过 NSFetchResultController 获取作业问题。我写了一个谓词来相应地提取问题。但是由于我希望问题基于 questionOrderIndex 进行排序,因此我通过问题实体对排序描述符的排序键是 assignmnetQuestion.questionOderIndex。

但是使用这个排序键,我得到一个异常:“Exception = to-many key not allowed here”。经过调查,我发现这是因为 Question 与 AssignmentQuestion 表之间存在“一对多”关系。

我的问题是:

1) 如何根据 questionOrderIndex 为排序描述符设置排序键?

2) 我们真的需要在核心数据中有一个连接表来按顺序对问题进行排序吗?

获取问题的谓词:

NSString *predicateString = [NSString stringWithFormat:@"self.evaluationQuestion.assignmentEvaluation.assignmentId == %@", @"1"]; 

最佳答案

如果您不需要监听问题的变化(在您的 View Controller 之外),您可以取而代之获取 AssignmentQuestion 实体,并预取附加的问题。
稍后,在您的 View Controller 中,不使用 AssignmentQuestion 实体,而是使用它们预取的 question 关系

//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]]];

还有see here

关于iphone - 在 NSFetchedResultsController 中设置 NSSortDescriptor 键的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760425/

相关文章:

iphone - 加密和签名创建

iphone - 将基于自定义 View 的 UIBarButtonItem 放置在没有默认水平填充的导航栏中

ios - 解析数据与解析云代码选项卡

ios - SpriteKit : SKScene Freeze when modal view pesented

ios - 在 UISplitView 中替换我的 detailViewController 在旋转我的应用程序 Objective-C 时崩溃

iphone - iOS SDK : playing music on the background and switching views

iphone - NSOperationQueue 感觉比在主线程上运行代码慢

ios - 核心数据合并政策

iphone - dequeueReusableCellWithIdentifier :identifier not picking up a loadNibNamed cell

iphone - EKEvents Store 对象初始化