简体   繁体   中英

NSPredicate compare dates in CoreData

I have a managed object with two dates: dateOne and dateTwo and I want to retrieve the dates where dateTwo is older than dateOne.

Right now I am using the following predicate to fetch the objects:

NSArray *objects = nil;

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"dateOne > dateTwo", @""];

objects = [context executeFetchRequest:request error:nil];
[request release];

Sometimes it works, but other times the objects fetched have a dateTwo that is more recent than dateOne. Is there a better way to do something like this?

Thanks!

也许您可以向实体添加BOOL属性“ dateTwoIsOlder”,然后仅获取遵守该属性的对象?

为什么不只从核心数据创建fetch request ,而不能以编程方式进行呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM