瞬态sectionNameKeyPath& NSSortDescriptor NSFetchedResultsController [英] Transient sectionNameKeyPath & NSSortDescriptor NSFetchedResultsController

查看:99
本文介绍了瞬态sectionNameKeyPath& NSSortDescriptor NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Core Data中有一个任务列表。我使用NSFetchedResultsController将它们提取到UITableView中。

I have a list of tasks within Core Data. I fetch them into a UITableView using an NSFetchedResultsController.

我需要自定义顺序的自定义部分:

I need custom sections in a custom order:


  1. OVERDUE

  2. ACTIVE

  3. ONGOING

  4. POSTPONED

  5. 已完成

  1. OVERDUE
  2. ACTIVE
  3. ONGOING
  4. POSTPONED
  5. COMPLETED

要确定任务应该进入哪个部分,我会使用基于其他方式动态计算的派生瞬态属性相对对象中的属性。

To determine what section a task should go in I use a derived transient attribute calculated on the fly based on other attributes in the relative object.

不幸的是,您无法将派生值作为获取请求使用的排序描述符传递。这是因为fetch依赖于已经要求获取的数据。鸡肉和鸡肉鸡蛋。

Unfortunately you cannot pass a derived value as a sort descriptor used by a fetch request. This is because a fetch relies on already having data it is being asked to fetch. Chicken & Egg.

我理解为什么我不能这样做,这对我没有帮助解决问题!

I understand why I can't do it, that doesn't help me solve the problem!

我试过了:


  • 将NSFetchedResultsController子类化为'自定义创建
    部分和索引标题
    。也许我做错了,但这只是改变了部分的名称和顺序,而不是那些部分的内容(这很关键)。

  • Subclassing NSFetchedResultsController to 'customise creation of sections and index titles'. Maybe I'm doing this wrong however this just changes the names and orders of sections, not how many things go in those sections (which is critical).

每个部分填充数组并将它们提供给表格(笨重,
缓慢但完全有效)。

Populating arrays per section and feeding them to the table (clunky, slow yet fully works).

剔除正在进行的&推迟的任务(有效但不理想)。这样我可以通过dueDate排序并通过瞬态值驱动sectionNameKeyPath。

Ripping out ongoing & postponed tasks (which works, but isn't ideal). This way I can sort by dueDate and drive the sectionNameKeyPath via the transient values.

有没有人有更好的想法?有很多问题就像这个问题一样,但没有一个问题可以解决。

Does anyone have any better ideas? There are quite a few questions already just like this one yet none of them come to a neat solution.

提前致谢!

干杯

推荐答案

最简单的解决方案是添加持久的section属性(或转换现有的transient属性) )。使它成为一组可排序的值(使用您想要的名称在运行时显示)。每当你更新任何记录时,也要重新计算并存储section属性。

The simplest solution is to add a persistent 'section' attribute (or convert your existing transient attribute). Make it a set of sortable values (which you display at runtime using the names you want). Whenever you update any record, recompute and store the section attribute as well.

我意识到这听起来像是在存储冗余信息,但除了使你的逻辑更简单之外,它还会也是可索引的,可以加快访问速度。

I realize this sounds like you're storing redundant information but besides making your logic simpler, it will also be indexable which will speed access.

这篇关于瞬态sectionNameKeyPath& NSSortDescriptor NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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