开发者

NSPredicate compare dates in CoreData

开发者 https://www.devze.com 2023-02-21 03:52 出处:网络
I have a managed object with two dates: dateOne and dateTwo and I want to retrieve the dates where dateTwo is older than dateOne.

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

开发者_StackOverflow社区

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!


Perhaps you could add a BOOL property "dateTwoIsOlder" to the entity, then just fetch objects that adhere to that?


Why not just create a fetch request from core data and not do it programmatically?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号