Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
NSFetchedResultsController with custom NSSortDescriptor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSFetchedResultsController with custom NSSortDescriptor


I am using an NSFetchedResultsCountroller to execute a query against a
Core Data database backed by a sqlite data store. I would like to sort
the results by an NSString property of my NSManagedObject. However, if
the property is nil or an empty string, I would like those results to
appear at the end of my UITableView instead of the beginning. (Think
sorting in iTunes by artist. I want the songs with no artist to appear
at the end instead of the beginning of my list.)

The first thing I tried to do was to create a category on NSString and
create a new comparison method which would sort exactly how I want.
This did not work and gave me the following error message:

*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor
selector: myComparisonMethod:'

I believe this is because the sorting is done by the database and Core
Data has a very explicit list of allowed selectors for
NSSortDescriptors in my situation.

The second solution I considered involved manually sorting the results
in-memory. However, that would make me lose out on a lot of
NSFetchedResultsController functionality as I would no longer be able
to use it for UITableView sections or automatic updating of edited
NSManagedObjects. That also seems inefficient as
NSFetchedResultsCountroller doesn't necessarily bring all of the
results into memory at the same time and only loads objects on an
as-needed basis.

The final solution I considered involved modifying the data stored in
the database such that instead of storing nil or blank for this
property, it would instead store something that would most likely sort
to the end of the list (like @"zzzzzzzz"). This, however, seems like a
big hack and would necessitate changing a lot of my code to account
for this. That, and I do not like the idea of messing with the data in
my database just to get this sorting problem to work.

Finally, I thought about using two NSFetchedResultsController instead
of one. One for all entities that have this property set, and one for
all that do not. I'd prefer not to do this as it would overly
complicate a lot of my code. If I could just get the data to come back
sorted correctly, all of my existing code would "just work". However,
of all the ideas I've had so far, I think that this one is the best.

Am I missing something here? Is there a better way to perform this
kind of sort? Does anyone have any recommendations? Maybe there is a
way to use two NSSortDescriptors? One that will first sort on the
emptiness of the property and a second that will sort on the property

Many thanks,

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.