16

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. 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.

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!

I've tried:

  • 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).

  • 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.

Thanks in advance!

Cheers

Timbo
  • 1,214
  • 1
  • 16
  • 32
  • 3
    One option could be - if you are already fetching all tasks from Core data - maybe you could save extra atributes for each task - and if task "on the fly" changes section - it could also be updated in Core Data - thus - with animation repositioned in correct section by controllerWillChangeContent: delegate methods? Or am I missing something? – Guntis Treulands May 13 '12 at 20:58

1 Answers1

11

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.

Denis Hennessy
  • 7,243
  • 4
  • 25
  • 31
  • Hi Denis, thanks for your response. As changing dates will move items into different sections, I wonder how I would check/change the section attribute efficiently? – Timbo May 16 '12 at 10:27
  • This Answer + Guntis Treulands' comment on my question works great :) – Timbo May 16 '12 at 20:36