I have a Event
table that saves a NSString *action
and a NSDate *date
. I want to filter the array retrieved from core data to display all the actions performed in the last hour and the next hour not depending on the day it was saved. Example on Tuesday a action was saved on 16:00 but Today is Friday 17:00 and I still want to retrieve that action saved on Tuesday and any action within that time frame.
Asked
Active
Viewed 377 times
0

Johan de Klerk
- 2,515
- 3
- 25
- 35
1 Answers
0
For anyone stuck with this problem. Changed my Event Table to hold a integer (hour of the action) and retrieved it like this
NSDate *currentFullDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentFullDate];
NSInteger currentHour = [components hour];
NSInteger hourAgo = currentHour -1;
NSInteger nextHour = currentHour +1;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
request.predicate = [NSPredicate predicateWithFormat:@"(hour >=%@) AND (hour <= %@)", hourAgo, nextHour];
request.fetchLimit = 5;
NSError *error;
NSArray *eventArray = [managedObjectContext executeFetchRequest:request error:&error];
for (Event *event in eventArray)
{
}

Johan de Klerk
- 2,515
- 3
- 25
- 35