Timbo
Timbo

Reputation: 1214

Transient sectionNameKeyPath & NSSortDescriptor NSFetchedResultsController

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:

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

Upvotes: 16

Views: 2893

Answers (1)

Denis Hennessy
Denis Hennessy

Reputation: 7463

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.

Upvotes: 11

Related Questions