基于有序多对关系的排序描述符 [英] Sort Descriptor based on ordered to-many relationship

查看:52
本文介绍了基于有序多对关系的排序描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的核心数据模型的描述:




  • 项目和问题实体

  • 项目有一个订购了一对多关系到名为问题的问题

  • 问题与名为parentProject的项目具有一对一关系



这是我获取问题的代码:

  let fetchRequest = NSFetchRequest (实体名称:问题)
fetchRequest.predicate = NSPredicate(格式: parentProject CONTAINS [cd]%@,argumentArray:[project])
fetchRequest.sortDescriptors = [NSSortDescriptor(key: name ,升序:true)]

let frc = NSFetchedResultsController(
fetchRequest:fetchRequest,
managedObjectContext:dataManager.context,
sectionNameKeyPath:nil,
cacheName:无)

返回弧$ ​​b $ b

即使我遇到了所有问题项目对象,我更喜欢使用提取的re sults控制器,因此它们将始终被更新,我喜欢与表的集成。我也喜欢在导航控制器屏幕中使用FRC来显示项目之前。



但是我希望按照将它们保留在项目的NSMutableOrderedSet中的顺序对它们进行排序。



AFAIK当用于核心数据时,我不能将NSSortDescriptor与比较器/选择器一起使用。



您知道怎么做吗?预先感谢。

解决方案

您可能不想听,但是很遗憾,您不能。 NSFetchedResultsController不能基于排序的一对多关系使用sortDescriptor进行排序。您应该向Apple提交错误。您应该放弃NSFetchedResultsController并自己处理其委托方法。


Description of my core data model:

  • Project and Issues entities
  • Project has an ordered one-to-many relationship to Issues named issues
  • Issue has one-to-one relationship with Project named parentProject

Here is my code to obtain issues:

let fetchRequest = NSFetchRequest(entityName: "Issue")
    fetchRequest.predicate = NSPredicate(format: "parentProject CONTAINS[cd] %@", argumentArray: [project])
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]

    let frc = NSFetchedResultsController(
        fetchRequest: fetchRequest,
        managedObjectContext: dataManager.context,
        sectionNameKeyPath: nil,
        cacheName: nil)

    return arc

Even though I have all issues from the project object, I prefer to obtain issues using fetched results controller, so they will always be updated and I like the integration with tables. I also like that in navigation controller screen before the projects are displayed using FRC as well.

As you see in the code the Issues are sorted by the name parameter.

However I'd like them to be sorted by the order I keep them in the NSMutableOrderedSet of project.

AFAIK I cannot use NSSortDescriptor with comparator/selector when it's used to Core Data.

Do you know how to do it? Thanks in advance.

解决方案

You may not want to hear this, but unfortunately, you can't. NSFetchedResultsController cannot be sorted using sortDescriptors based on the ordered one-to-many relationship. You should file a bug with Apple. You should ditch your NSFetchedResultsController and handle it's delegate methods yourself.

这篇关于基于有序多对关系的排序描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆