使用类别和核心数据的自定义排序 - 是否支持框架? [英] Custom sorting using categories and core data - is it supported by the framework?

查看:190
本文介绍了使用类别和核心数据的自定义排序 - 是否支持框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iPhone iOS 4.0,我有一个使用Core Data存储的生日对象(日期+名称)的列表。我想使用NSFetchedResultsController检索此列表,按照每个生日对象的下一个出生日期排序。为此,我需要为排序描述符定义我自己的逻辑。



我尝试使用:

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

其中 compareNextBirthday:我创建了一个NSDate类。



但是当我尝试获取数据时,我得到以下错误:unsupported NSSortDescriptor selector:compareNextBirthday:



我花了几个小时试图找出没有运气... Core Data是否支持这种自定义排序? cn> NSFetchedResultsController code>在持久存储级别执行,所以如果你使用SQLite作为后端它将失败。



对于这样的东西,除了实际的出生日期之外,我还将对数据进行反标准化并存储月份和日期,以便对它们进行排序。 p>

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?

解决方案

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.

这篇关于使用类别和核心数据的自定义排序 - 是否支持框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆