ZappyCode
ZappyCode

Reputation: 75

Custom Sort on NSFetchRequest

When performing a NSFetchRequest on Core Data, I want to sort the results based off a custom selector instead of one of the entity's attributes. My best solution right now is to use a NSSortDescriptor on the results after the fetch has been performed. This isn't ideal for me though because I would like to use a NSFetchedResultsController, and with a NSFetchedResultsController I wouldn't be able to run a NSSortDescriptor on the results after they were fetched.

Upvotes: 1

Views: 658

Answers (2)

J2theC
J2theC

Reputation: 4452

Using a custom selector for your sorting means you will need to sort the items in memory. If you potentially have a large database this could mean some performance troubles for your application. Maybe try to perform the calculation of the attribute on insertion and expanding the result into a new attribute for your entity.

Upvotes: 2

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

Assuming you are using SQLite as your store, you are limited by what you can sort with. The limitation is what can be turned into a SQLite order by.

In your case you are probably better off using a NSFRC as your monitor of changes and then put a NSArray in front of it that is sorted the way you want. This will require you to do some clever work whenever a change comes in to minimize your table redraws but it shouldn't be too onerous.

Upvotes: 2

Related Questions