核心数据NSPredicate不在两个日期之间返回记录 [英] Core Data NSPredicate not returning records between two dates
问题描述
我有一个CoreData实体注释
谁有一个父实体 Measurement
。
I have a CoreData entity Comment
who has a parent entity Measurement
.
测量
实体具有 measurementDate
属性。
我的Core Data数据库中有200条注释
记录,每条记录都有 measurementDate
的值。
My Core Data database has 200 Comment
records in it, each with varying measurementDate
values over the last 3 years.
我试图获取 measurementDate
I'm trying to get the records where measurementDate
is between any two given dates.
首先,我尝试过:
//now fetch a record between 12 and 11 months ago
NSDate *startDate = [AppGlobals dateByAddingMonths:[NSDate date] withMonths:-12];
NSDate *endDate = [AppGlobals dateByAddingMonths:startDate withMonths:1];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(measurementDate >= %@) AND (measurementDate <= %@)", startDate, endDate];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Comment" inManagedObjectContext:self.context]];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [self.context executeFetchRequest:request error:&error];
但我有0个结果。
所以我试着用更简单的代替我的谓词:
So then I tried replacing my predicate with something simpler:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"measurementDate >= %@", startDate];
这应该会返回大约三分之一的结果。但实际上它返回所有200个记录。
This should return approximately a third of my results. But actually it returns ALL 200 RECORDS.
我已经注销对象的日期,它看起来像日期被完全忽略!
I've logged out the dates of the objects and it looks like the dates are being completely ignored!
我做错了什么?
这是与父抽象实体<$实体而不是测量$ c $实体获取
c> entity?
是因为我在我的代码中使用标量属性原始数据类型?
Is it something to do with the Parent Abstract entity Measurement
and because I'm fetching from the Comment
entity and not the Measurement
entity?
Is it because I'm using "scalar properties for primitive data types" in my code?
任何帮助非常感谢。
推荐答案
是您将日期值分配给托管对象的位置。如果 measurementDate
定义为标量属性
I assume that the problem is where you assign the date value to the managed object. If measurementDate
is defined as scalar property
@property (nonatomic) NSTimeInterval measurementDate;
然后你分配一个 NSDate
NSDate *theDate;
obj.measurementDate = [theDate timeIntervalSinceReferenceDate];
这篇关于核心数据NSPredicate不在两个日期之间返回记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!