CoreData 按即将到来的生日排序

发布于 2024-10-06 19:11:54 字数 1312 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-10-13 19:11:55

这是你可以做的。

1) 为您的 uBDay 和 uName 设置 NSSortDescriptors ,以便您的记录将首先按生日排序,然后按姓名排序。

2) 创建一个 NSPredicate 来提取 [NSDate now] (即今天)到未来某个时间(即未来 30 天 = [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30])

示例代码如下:

#define kOneDayTimeInterval 86400 // this is in seconds: 86400 = 1 day

    - (NSFetchedResultsController *)fetchedResultsController {
        ...
        NSSortDescriptor *sortByBDay = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
        NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"uName" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByBDay,sortByName, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [sortDescriptors release];
        [sortByName release];
        [sortByReleaseDate release];            
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(uBDay > %@) AND (uBDay <= %@)", [NSDate date], [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30]];
        [fetchRequest setPredicate:predicate];
        ...

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:

#define kOneDayTimeInterval 86400 // this is in seconds: 86400 = 1 day

    - (NSFetchedResultsController *)fetchedResultsController {
        ...
        NSSortDescriptor *sortByBDay = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
        NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"uName" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByBDay,sortByName, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [sortDescriptors release];
        [sortByName release];
        [sortByReleaseDate release];            
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(uBDay > %@) AND (uBDay <= %@)", [NSDate date], [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30]];
        [fetchRequest setPredicate:predicate];
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文