I’m writing an app that displays some data which changes based upon the day of the week it is. The context of the app is that it is for a conference that is upcoming. I want to display the calendar entries for that day. The event detail being stored in coredata.
What I’m using to create the NSPredicate is:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *today = [NSDate date];
NSString *sunday = @"2011-12-04";
NSString *monday = @"2011-12-05";
NSString *tuesday = @"2011-12-06";
NSString *wednesday = @"2011-12-07";
NSInteger dayNumber = 1;
if ([[df stringFromDate:today] isEqualToString:sunday]) {
dayNumber = 2;
} else if ([[df stringFromDate:today] isEqualToString:monday]) {
dayNumber = 3;
} else if ([[df stringFromDate:today] isEqualToString:tuesday]) {
dayNumber = 4;
} else if ([[df stringFromDate:today] isEqualToString:wednesday]) {
dayNumber = 5;
}
NSExpression *lhs = [NSExpression expressionForKeyPath:@"day_number"];
NSExpression *rhs = [NSExpression expressionForConstantValue:[NSNumber numberWithInt:dayNumber]];
NSPredicate *equalToPredicat = [NSComparisonPredicate predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSEqualToPredicateOperatorType
options:0];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptorTime = [[NSSortDescriptor alloc] initWithKey:@"start_time" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:/*sortDescriptorDayNumber,*/ sortDescriptorTime, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:equalToPredicat];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"day_number"
cacheName:@"TodayCache"]
What I am seeing though is that when the date changes (ie: Saturday to Sunday) the data (displayed in a UITableView) doesn’t get updated.
Thanks,
Matt.
The comment above is correct. Some more info.
1) The table will only update when
[tableView reloadData]or one of the other reload methods is called2) If you set a delegate for the NSFetchedResultsController you can respond to model changes and reload the table
3) The date changing is not a model change (unless you are updating the date in the model itself) and the NSFetchedResultsController will only update itself if it detects that it’s managed object context has changed.
4) You need to do
fetchedResultsController:performFetch:errorwhen when the day changes b/c you are changing the predicate (i.e. the request). Otherwise, the table will reload with the old data.To refresh the table based on the date changing, you need to detect the date change at whatever interval you like and reload the table at that time. One way you could do it is to calculate the interval between the current time and midnight and set a timer to fire after that interval. When the timer fires, change the predicate, performFetch with the results controller and reload the table. Then set a new timer until the next midnight. If you want this to be done in the users time zone, you’ll need to take that into account also.
EDIT: clarified answer that a
performFetchis necessary when the predicate changes