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

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

问题描述

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

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.

我尝试使用:

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

其中 compareNextBirthday: 是在我在 NSDate 类上创建的类别中定义的.

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

但是当我尝试获取数据时出现以下错误:不支持的 NSSortDescriptor 选择器:compareNextBirthday:"

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

我花了几个小时试图解决这个问题,但没有运气... Core Data 是否支持这种自定义排序?我真的需要进行内存排序吗?

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?

推荐答案

NSFetchedResultsController 内部的排序是在持久存储级别执行的,因此如果您使用 SQLite 作为后端,它将失败.

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天全站免登陆