DBAccess 框架 iOS NSDate 查询失败
DBAccess framework iOS NSDate failed to query
我正在使用 DBAccess
框架,我的一个模型存储使用 [NSDate date]
创建的记录的日期,并使用 DBAccess
框架存储到数据库。现在,当我尝试使用查询进行检索时,它失败了。我只想检索最近 7 天的记录并编写了类似
的查询
DBResultSet* result = [[[weightModel query] whereWithFormat:@"savedAt >= %@ AND savedAt <= %@", beforeDate, beforeDate] fetch];
其中 beforeDate 是 NSDate
是
-(NSDate*)getDateFor:(int)daysBack {
NSDate* date = [NSDate date];
NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.day = -daysBack;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* beforeDate = [calendar dateByAddingComponents:comps toDate:date options:0];
return beforeDate;
}
谁知道我怎样才能得到最近 7 天的 NSDate
记录?
但是,我已经使用
确认了这些记录的存在
weightModel* weightRecord = [[[weightModel query] fetch] objectAtIndex:i];
[dates weightRecord.savedAt];
并打印出来。
最初我回答了你的问题,说你生成和查询日期的方式似乎没有问题。但是,我没有注意到一个小细节,那就是您正在查询日期 >= && <= ,其中参数是相同的日期值。
鉴于 NSDate
对象的精度大于 1 秒,这意味着您的记录必须在 7 天前的同一毫秒内存在(*需要引用,不确定实际精度) .这不太可能 return 任何结果。
您需要做的是创建一个日期函数,return是一天的开始和一天的结束。
或者,您可以使用 SQLite
datetime
函数为您完成此操作。但我建议将其保留在 obj-c 中。
谢谢
阿德里安
我正在使用 DBAccess
框架,我的一个模型存储使用 [NSDate date]
创建的记录的日期,并使用 DBAccess
框架存储到数据库。现在,当我尝试使用查询进行检索时,它失败了。我只想检索最近 7 天的记录并编写了类似
DBResultSet* result = [[[weightModel query] whereWithFormat:@"savedAt >= %@ AND savedAt <= %@", beforeDate, beforeDate] fetch];
其中 beforeDate 是 NSDate
是
-(NSDate*)getDateFor:(int)daysBack {
NSDate* date = [NSDate date];
NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.day = -daysBack;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* beforeDate = [calendar dateByAddingComponents:comps toDate:date options:0];
return beforeDate;
}
谁知道我怎样才能得到最近 7 天的 NSDate
记录?
但是,我已经使用
确认了这些记录的存在weightModel* weightRecord = [[[weightModel query] fetch] objectAtIndex:i];
[dates weightRecord.savedAt];
并打印出来。
最初我回答了你的问题,说你生成和查询日期的方式似乎没有问题。但是,我没有注意到一个小细节,那就是您正在查询日期 >= && <= ,其中参数是相同的日期值。
鉴于 NSDate
对象的精度大于 1 秒,这意味着您的记录必须在 7 天前的同一毫秒内存在(*需要引用,不确定实际精度) .这不太可能 return 任何结果。
您需要做的是创建一个日期函数,return是一天的开始和一天的结束。
或者,您可以使用 SQLite
datetime
函数为您完成此操作。但我建议将其保留在 obj-c 中。
谢谢 阿德里安