NSDate的核心数据提取谓词 [英] Core data fetch predicate for NSDate

查看:291
本文介绍了NSDate的核心数据提取谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个NSDate属性的实体:

I have entity with two NSDate properties:

@property NSDate *startTime;
@property NSDate *endTime;

我使用它来创建点事件和持续时间事件。因此,对于点事件 endTime 可以等于nil。

I use it for creating the point events and the duration events. So, for the point events endTime can be equal to nil.

而且我想获取限制的所有实体:startTime> = minDate&& endTime <= maxDate

And i want to fetch all entities with restriction: startTime >= minDate && endTime <= maxDate

但是NSPredicate像:

But NSPredicate like:

NSDate *startTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstBound"];
NSDate *endTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"secondBound"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(startTime >= %@) AND ((endTime <= %@) OR (endTime == nil))",startTime,endTime]];

无法正常工作。我会解释。如果数据库中存储的所有事件都在2000-2010年的范围内,并且我使用minDate = 1900,maxDate = 1950进行提取,则来自数据库的点事件适合此谓词,因为它们的endTime字段为nil,并且字段的startTime明显大于minDate。

Does not work correctly. I`ll explain. If all the events stored in the database are in the range 2000-2010 year, and I'm doing a fetch with minDate = 1900, maxDate = 1950 EVERYTHING the point events from the database fit this predicate, because they endTime field is nil, and the field startTime clearly more than minDate.

任何想法如何处理这个?不受NSPredicate的限制。

Any idea how to handle this? Do not be limited by the NSPredicate. I will approach any decision.

推荐答案

可能你想要:

"((endTime != nil) and (startTime >= %@) and (endTime <= %@)) or
((endTime = nil) and (startTime >= %@) and (startTime <= %@))",
startTime, endTime, startTime, endTime

$ b b

虽然如果你将瞬时事件存储为具有相同的开始和结束时间会更容易。

Though it'd be easier if you'd store instantaneous events as just having the same start and end time.

这篇关于NSDate的核心数据提取谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆