作为获取谓词的日期范围 - 核心数据之间的 NSDate 谓词
我正在尝试获取存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来很像你的变量
startDate
和endDate
会包含意外的值。startDate
来自没有日期等的日期组件,而endDate
来自startDate
。因此,我建议在谓词定义之前放置断点或 NSLog 语句,以便检查这些值。
另外,尚不清楚您使用的是什么
NSCalendar
。来自因此,例如将
day
属性设置为0
似乎可能会出现问题。This looks a lot like your like your variables
startDate
andendDate
would contain unexpected values. ThestartDate
is from date components without a day etc., and theendDate
from thestartDate
.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:So it seems that e.g. setting the
day
property to0
might be problematic.为谓词尝试不同的格式:
仅在单个语句周围放置括号,而不是在整个语句周围放置括号。
Try a different format for your predicate:
Only put paratheses around the single statements, not around the whole statement.
在 iOS 6 之后,如前所述,这个谓词起作用了。
谢谢。
After iOS 6, as said before, this predicate works.
Thank you.