CoreData 按即将到来的生日排序
我正在 xcode 中构建我的第一个应用程序,并尝试使用 coreData 和 NSFetchedResultController 获取按即将到来的生日排序的人员列表。我的实体设置如下
uBirthdays
--------------
NSString uName
NSDate uBday
这是我当前的代码:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"uBirthdays" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionNameGen" cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
是否可以按即将到来的生日对其进行排序?或者我是否必须将我的生日非标准化,以便我只能按月和日排序?任何意见都将受到高度赞赏。
谢谢
I'm building my first app in xcode and trying to fetch a list of people ordered by upcoming birthdays using coreData and NSFetchedResultController. My entity is setup as follows
uBirthdays
--------------
NSString uName
NSDate uBday
Here is my current code:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"uBirthdays" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionNameGen" cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
Is it possibly to sort that by upcoming birthdays? or would I have to de-normalize my birthdays so I can sort by month and day only? Any input is greatly appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你可以做的。
1) 为您的 uBDay 和 uName 设置
NSSortDescriptors
,以便您的记录将首先按生日排序,然后按姓名排序。2) 创建一个
NSPredicate
来提取[NSDate now]
(即今天)到未来某个时间(即未来 30 天 =[NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30])
示例代码如下:
Here's what you can do.
1) Set
NSSortDescriptors
for your uBDay and uName so your records will be sorted first by birthday, then by name.2) Create a
NSPredicate
that pulls all birthdays between[NSDate now]
(i.e. today) up to a certain time in the future (i.e. 30 days into the future =[NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30]
)Example code below: