Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm developing for iPhone iOS 4.0 and I have a list of birthday objects (date + name) stored using Core Data. I want to use the NSFetchedResultsController to retrieve this list sorted by the next birthdate date of each birthday object. For that I need to define my own logic for the sort descriptor.

I tried using:

[NSSortDescriptor sortDescriptorWithKey:@"birthdayDate" 
                              ascending:YES 
                               selector:@selector(compareNextBirthday:)];

Where compareNextBirthday: is defined in a category I created on a NSDate class.

But when I try to fetch the data I get the following error: "unsupported NSSortDescriptor selector: compareNextBirthday:"

I spent hours trying to figure this out without luck... does Core Data support this kind of custom sorting at all? Do I really need to do an in-memory sort?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
886 views
Welcome To Ask or Share your Answers For Others

1 Answer

Sorting inside of the NSFetchedResultsController is performed at the persistent store level so if you are using SQLite as the backend it will fail.

For something like this I would de-normalize the data and store the month and day of month in addition to the actual birthdate so that you can sort on them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...