I have a Core Data model with an NSDate
property. I want to filter the database by day. I assume the solution will involve an NSPredicate
, but I'm not sure how to put it all together.
I know how to compare the day of two NSDate
s using NSDateComponents
and NSCalendar
, but how do I filter it with an NSPredicate
?
Perhaps I need to create a category on my NSManagedObject
subclass that can return a bare date with just the year, month and day. Then I could compare that in an NSPredicate
. Is this your recommendation, or is there something simpler?
Given a NSDate * startDate and endDate and a NSManagedObjectContext * moc:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:moc]];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
Example on how to also set up startDate and endDate to the above given answer:
...
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
//create a date with these components
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:1];
[components setDay:0]; //reset the other components
[components setYear:0]; //reset the other components
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
predicate = [NSPredicate predicateWithFormat:@"((date >= %@) AND (date < %@)) || (date = nil)",startDate,endDate];
...
Here I was searching for all entries within one month. It's worth to mention, that this example also shows how to search 'nil' date-entires.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With