[英]Core Data predicate for finding x days past since date
I have core data model: 我有核心数据模型:
Book
----
name
release_date
I need to get all books that at least two days passed from their release_date, 我需要获取从release_date起至少两天过去的所有图书,
And I want to do it with NSPredicate
(not to load all of the books to memory, and than loop and check) 我想用
NSPredicate
做到这NSPredicate
(不要将所有书籍都加载到内存中,而不是循环检查)
What I'm looking for is something like this: 我正在寻找的是这样的:
NSDate *now = [NSDate date];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"release_date + "48 hours" <= %@", now];
The predicate will not change the date for you. 谓词不会为您更改日期。 You should create the appropriate date (based on the current date, and using
NSDateComponents
) and pass that in to the predicate. 您应该创建适当的日期(基于当前日期,并使用
NSDateComponents
)并将其传递给谓词。 You can use <=
in the predicate for filter for older dates. 您可以在谓词中使用
<=
过滤旧日期。
In other words, don't think of the problem as: 换句话说,不要认为问题是:
@"release_date + "48 hours" <= %@", now
think of it as: 认为是:
@"release_date <= %@", now - "48 hours"
so you problem is just to get the now - "48 hours"
date to pass to the predicate. 因此,您的问题只是要将
now - "48 hours"
日期传递给谓词。
I think you want something like that: 我想您想要这样的东西:
NSDate *date2DaysBefore = [NSDate dateWithTimeIntervalSinceNow:-2*24*3600];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"release_date <= %@", date2DaysBefore];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.