作为获取谓词的日期范围 - 核心数据之间的 NSDate 谓词

发布于 2024-12-11 13:49:06 字数 1676 浏览 0 评论 0原文

我正在尝试获取存储在 Core Data 中的核心数据项,该核心数据项过滤为 eventStartDate 字段下的日期范围(日期类型) - 我正在使用 Xcode 生成的名为 Event 的托管对象来代理核心数据项。当我执行获取请求时,没有返回任何内容。我尝试查看 SQL 日志,发现正在调用以下 sql,这看起来是正确的:

SELECT 0, t0.Z_PK FROM ZEVENT t0 WHERE ( t0.ZEVENTSTARTDATE > ? AND  t0.ZEVENTSTARTDATE <= ?) ORDER BY t0.ZEVENTSTARTDATE

我什么也没得到,当我尝试 po [mgtEventArray count] 时,我在对象上得到 nil 。

我的获取代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:10];
[components setDay:0]; 
[components setYear:0]; 
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((eventStartDate > %@) AND (eventStartDate <= %@))",startDate,endDate];


NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescKey ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

NSError *error = nil;
NSArray *mgtEventArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if(error != nil)
    NSLog(@"%@", error);
return mgtEventArray;

I am trying to fetch core data items stored in Core Data filtered to a range of dates under the field eventStartDate which is Date type - I am using managed object called Event generated by Xcode to proxy the core data item. When I execute the fetch request nothing is returned. I tried looking at the SQL logs and see that the follow sql is being called, which look correct:

SELECT 0, t0.Z_PK FROM ZEVENT t0 WHERE ( t0.ZEVENTSTARTDATE > ? AND  t0.ZEVENTSTARTDATE <= ?) ORDER BY t0.ZEVENTSTARTDATE

I get nothing back, when i try to po [mgtEventArray count] I get nil on the object.

my code for fetching:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:10];
[components setDay:0]; 
[components setYear:0]; 
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((eventStartDate > %@) AND (eventStartDate <= %@))",startDate,endDate];


NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescKey ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

NSError *error = nil;
NSArray *mgtEventArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if(error != nil)
    NSLog(@"%@", error);
return mgtEventArray;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笨笨の傻瓜 2024-12-18 13:49:06

这看起来很像你的变量 startDateendDate 会包含意外的值。 startDate 来自没有日期等的日期组件,而 endDate 来自 startDate

因此,我建议在谓词定义之前放置断点或 NSLog 语句,以便检查这些值。

另外,尚不清楚您使用的是什么NSCalendar。来自

日、周、工作日、月和年数字通常从 1 开始,但是
可能存在特定于日历的例外情况。

因此,例如将 day 属性设置为 0 似乎可能会出现问题。

This looks a lot like your like your variables startDate and endDate would contain unexpected values. The startDate is from date components without a day etc., and the endDate from the startDate.

Thus, I recommend putting breakpoints or NSLog statements before your predicate definition in order to check these values.

Also, it is not clear what NSCalendar you are using. From the documentation:

Day, week, weekday, month, and year numbers are generally 1-based, but
there may be calendar-specific exceptions.

So it seems that e.g. setting the day property to 0 might be problematic.

囚我心虐我身 2024-12-18 13:49:06

为谓词尝试不同的格式:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eventStartDate > %@) AND (eventStartDate <= %@)",startDate,endDate];`

仅在单个语句周围放置括号,而不是在整个语句周围放置括号。

Try a different format for your predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eventStartDate > %@) AND (eventStartDate <= %@)",startDate,endDate];`

Only put paratheses around the single statements, not around the whole statement.

少钕鈤記 2024-12-18 13:49:06

在 iOS 6 之后,如前所述,这个谓词起作用了。

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate > %@",[NSDate date];`

谢谢。

After iOS 6, as said before, this predicate works.

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate > %@",[NSDate date];`

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文